python graphviz中文测试
from graphviz import Digraph# 创建有向图,使用 circo 布局以形成环形
dot = Digraph(comment='动物棋吃关系环形图')
dot.attr('node', fontname='SimHei', fontsize='14') # 设置中文字体
dot.attr('edge', fontname='SimHei')# 动物列表(按吃的关系顺序)
animals = ['老鼠', '猫', '狗', '狼', '豹', '老虎', '狮子', '大象']# 颜色列表(与动物对应)
colors = ['#FFD700', # 老鼠 - 金色'#FF6347', # 猫 - 番茄红'#4169E1', # 狗 - 深天蓝'#708090', # 狼 - 石板灰'#FF4500', # 豹 - 橙红'#DAA520', # 老虎 - 金菊'#B8860B', # 狮子 - 暗金'#A9A9A9' # 大象 - 深灰
]# 添加节点(圆形,填充颜色)
for animal, color in zip(animals, colors):dot.node(animal, animal, shape='circle', style='filled', fillcolor=color, fontcolor='black')# 添加边,形成闭环
for i in range(len(animals)):dot.edge(animals[i], animals[(i + 1) % len(animals)])# 设置整体布局为 circo,使节点排列成圆形
dot.attr('graph', layout='circo')# 生成并渲染图像
dot.render('animal_cycle', format='png', cleanup=True, view=True)
print("已生成:animal_cycle.png")
win安装:
1.先安装软件
Download | GraphvizSource Code Source code packages for the latest stable and development versions of Graphviz are available, along with instructions for anonymous access to the sources using Git.Executable Packages Packages marked with an asterisk(*) are provided by outside parties. We list them for convenience, but disclaim responsibility for the contents of these packages.Linux Precompiled binaries are available attached to releases on Gitlab, https://gitlab.com/graphviz/graphviz/-/releases. You may also find it useful to try one of the following third-party sites.https://graphviz.org/download/#windows软件安装后需要在cmd正常运行命令: dot --version
2.再在python环境中安装
pip install graphviz