安装流程
1.官网下载
官网下载地址:graphviz-2.3.8.msi
2.下载之后,进行安装。找到安装路径,如:F:\python-chajian_or_package\Graphviz
3.将其中bin文件夹对应的路径添加到path环境变量中,也就是将F:\python-chajian_or_package\Graphviz\bin添加到path环境变量中。

3.cmd 运行 pip install graphviz
4.测试cmd运行 dot -version

使用教程
例1:pymc3中用
1 2 | #import pymc3 as pm pm.model_to_graphviz(model).view() |
例2
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | def showBN(model,save=False): '''传入BayesianModel对象,调用graphviz绘制结构图,jupyter中可直接显示''' from graphviz import Digraph node_attr = dict( style='filled', shape='box', align='left', fontsize='12', ranksep='0.1', height='0.2' ) dot = Digraph(node_attr=node_attr, graph_attr=dict(size="12,12")) seen = set() edges=model.edges() for a,b in edges: dot.edge(a,b) if save: dot.view(cleanup=True) return dot showBN(model) |
例3
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | # coding:utf-8 from graphviz import Digraph dot = Digraph(comment='The Round Table') # 添加圆点 A, A的标签是 King Arthur dot.node('A', 'king') dot.view() #后面这句就注释了,也可以使用这个命令查看效果 # 添加圆点 B, B的标签是 Sir Bedevere the Wise dot.node('B', 'Sir Bedevere the Wise') #dot.view() # 添加圆点 L, L的标签是 Sir Lancelot the Brave dot.node('L', 'Sir Lancelot the Brave') #dot.view() #创建一堆边,即连接AB的边,连接AL的边。 dot.edges(['AB', 'AL']) #dot.view() # 在创建两圆点之间创建一条边 dot.edge('B', 'L', constraint='false') #dot.view() # 获取DOT source源码的字符串形式 print(dot.source) # 保存source到文件,并提供Graphviz引擎 dot.render('test-output/round-table.gv', view=True) |
运行结果:
