python graphviz的安装和简单作图

Graphviz (Graph Visualization Software) 使用DOT语言来描述图形的节点、连线及属性等,可以大大减少手绘图形调整图形格式的时间,而将主要精力放在图形的逻辑关系上。而且可以根据需要设置生成图像的格式,如PDF、JPG等。

以电脑配置Windows10,64位,anaconda环境下安装的python 3.5版本为例,下面的安装过程供参考。

一、软件和python包下载

先到官网下载安装软件
Graphviz官网安装包(msi)地址:https://graphviz.gitlab.io/_pages/Download/Download_windows.html
然后要配置环境变量
进入电脑——属性——高级配置——环境变量界面

1、先设置用户变量

设置用户变量

2、修改系统变量

进入系统变量的Path,然后新建,路径是graphviz的安装路径再加上\bin\dot.exe


系统变量

3、python安装graphviz包

我的电脑中安装了anaconda,所以直接用
conda install graphviz
下载的包被放在了C:\ProgramData\Anaconda3\Lib,这个路径要确保存在于用户的环境变量中

二、官方文档和参考

通过以下文档的介绍,我们能基本掌握常用的属性使用方法
User Guide
Examples
API reference
graph、node和edge的属性
学习graphviz小结
edge线条控制

三、基础知识

1、一个简单案例

流程就是用Digraph建立一个有向图,然后增加node和edge
The node()-method takes a name identifier as first argument and an optional label.如果有Label,做出的图就不显示name而是显示Label
The edge()-method takes the names of start node and end node,如果有label参数,就在线上显示标签内容

example = Digraph(comment='The Round Table')
example.node('A')#如果没有label,那么就显示为A
example.node('B', 'Sir Bedevere the Wise')
example.node('L', 'Sir Lancelot the Brave')

example.edges(['AB', 'AL'])#批量画边
example.edge('B', 'L', constraint='false')#单条边,从B指向L
example.view()
简单案例

如果不加node,直接用edge,系统也会默认先建立这些node


image.png

2、更改graph、node、edge的属性

Use the graph_attr, node_attr, and edge_attr arguments to change the default appearance of your graph, nodes, and edges.
例如要建立一个Digraph,并设置两个node,一条连接线,就可以在创建Digraph时规定Digraph的画布是从左到右展开(或从上到下‘TB’),连接线的形状是直线,node的形状是文本框,edge的箭头样式是vee

ps = Digraph(name='pet-shop', graph_attr={'rankdir':'LR','splines':'ortho'},node_attr={'shape': 'box'},edge_attr={'arrowhead':'vee'})
ps.node('parrot')
ps.node('dead')
ps.edge('parrot', 'dead',label='haha')#Label的
image.png

如要更新graph、node或edge的特征,就要用

ps.edge_attr.update(arrowsize='2')
ps.node_attr.update(style='filled', color='skyblue')
修改node和edge特征

3、先把图建好,直接指定graph、node、edge的属性

To directly add attritbute statements (affecting all following graph, node, or edge items within the same (sub-)graph), use the attr()-method with the target as first argument:
官网总结的很好,如果先直接影响同一个graph或subgraph下属的node或edge性质,可以在建图后,使用attr方法
例如

ni = Digraph('ni')#新建一个名为ni的图

ni.attr('node', shape='rarrow')#with the target as first argument,这里的属性修改目标是node,改为箭头型
ni.node('1', 'Ni!')#此时新建的node均为rarrow型
ni.node('2', 'Ni!')

ni.node('3', 'Ni!', shape='egg')#在新建node时直接指定形状为蛋型

ni.attr('node', shape='star')#修改node属性,形状为星星
ni.node('4', 'Ni!')#此时新建的node均为star
ni.node('5', 'Ni!')
ni.attr(rankdir='LR')#这里没有在第一个自变量处指定目标,因此修改目标是digraph,By omitting its first argument, you can use it to set arbitrary attributes as key-value pairs targeting the current (sub-)graph

ni.edges(['12', '23', '34', '45'])
ni.view()
image.png

4、subgraph

可以为当前的graph增加subgraph,有两种方法,一种直接指定:

p = Graph(name='parent')
p.edge('spam', 'eggs')
c = Graph(name='child', node_attr={'shape': 'box'})
c.edge('foo', 'bar')
p.subgraph(c)

更为推荐第二种, with a with-block (omitting the graph argument):

p = Graph(name='parent')
p.edge('spam', 'eggs')
with p.subgraph(name='child', node_attr={'shape': 'box'}) as c:
...    c.edge('foo', 'bar')
subgraph

5、图的查看

一般是直接用digraph的name.view()方法,但在修改digraph后,必须要关闭pdf才能再次调用view()方法,否则就无法查看,如果不想关闭,就要引入一个库tempfile,并在.view()的参数中加入tempfile.mktemp('.gv')。
You can use the mktemp() function from the stdlib tempfile module to render to a different file for each invocation to avoid needing to close the viewer window each time

import tempfile
g = Digraph()
g.node('spam')
g.view(tempfile.mktemp('.gv'))  
g.view(tempfile.mktemp('.gv'))

如果在Jupyter Notebook中直接查看

from graphviz import Source
Source.from_file('ni.gv')  
直接查看结果

最后一个例子,展示一下render的用法,render可以保存输出路径并展示图,是否需要打开展示可以用view控制,为了方便,我们可以在Jupyter notebook中直接查看

ni = Digraph('ni')
ni.attr('node', shape='rarrow')
ni.node('1', 'Ni!')
ni.node('2', 'Ni!')

ni.node('3', 'Ni!', shape='egg')

ni.attr('node', shape='star')
ni.node('4', 'Ni!')
ni.node('5', 'Ni!')
ni.node('6', 'Ni!')
ni.attr(rankdir='LR')

ni.edges(['12', '23', '34', '45'])
ni.render('ni.gv', view=False) 
# ni.view()
# ni.view(tempfile.mktemp('.gv'))
Source.from_file('ni.gv')  
直接展示图像
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
禁止转载,如需转载请通过简信或评论联系作者。