Graphviz 画图教程(Python)
作者:互联网
文章目录
前言
之前我的博客介绍了Graphviz 画图教程,虽然dot语法类似C语言容易编写和理解,但是这仅限于小图,当你想要画一个大图的时候,每一个结点都得一个个去定义名字、属性、连接线,这无疑是十分麻烦的,这种时候就想到了Python,能否利用Python语言编写一个画图脚本呢?
Graphviz库
幸运的是,Python正好有一个Graphviz库,Github地址
安装也是十分简单,只需要一行代码:
pip install graphviz
开始
Digraph(一)
本次博客的代码直接在Jupyter上运行,方便显示结果
from graphviz import Digraph
dot = Digraph(comment='The Round Table')
dot.node('A', 'King Arthur')
dot.node('B', 'Sir Bedevere the Wise')
dot.node('L', 'Sir Lancelot the Brave')
dot.edges(['AB', 'AL'])
dot.edge('B', 'L', constraint='false')
dot
Dot代码
// The Round Table
digraph {
A [label="King Arthur"]
B [label="Sir Bedevere the Wise"]
L [label="Sir Lancelot the Brave"]
A -> B
A -> L
B -> L [constraint=false]
}
Digraph(二)
from graphviz import Digraph
f = Digraph('finite_state_machine', filename='fsm.gv')
f.attr(rankdir='LR', size='8,5')
f.attr('node', shape='doublecircle')
f.node('LR_0')
f.node('LR_3')
f.node('LR_4')
f.node('LR_8')
f.attr('node', shape='circle')
f.edge('LR_0', 'LR_2', label='SS(B)')
f.edge('LR_0', 'LR_1', label='SS(S)')
f.edge('LR_1', 'LR_3', label='S($end)')
f.edge('LR_2', 'LR_6', label='SS(b)')
f.edge('LR_2', 'LR_5', label='SS(a)')
f.edge('LR_2', 'LR_4', label='S(A)')
f.edge('LR_5', 'LR_7', label='S(b)')
f.edge('LR_5', 'LR_5', label='S(a)')
f.edge('LR_6', 'LR_6', label='S(b)')
f.edge('LR_6', 'LR_5', label='S(a)')
f.edge('LR_7', 'LR_8', label='S(b)')
f.edge('LR_7', 'LR_5', label='S(a)')
f.edge('LR_8', 'LR_6', label='S(b)')
f.edge('LR_8', 'LR_5', label='S(a)')
f
Dot代码
digraph finite_state_machine {
rankdir=LR size="8,5"
node [shape=doublecircle]
LR_0
LR_3
LR_4
LR_8
node [shape=circle]
LR_0 -> LR_2 [label="SS(B)"]
LR_0 -> LR_1 [label="SS(S)"]
LR_1 -> LR_3 [label="S($end)"]
LR_2 -> LR_6 [label="SS(b)"]
LR_2 -> LR_5 [label="SS(a)"]
LR_2 -> LR_4 [label="S(A)"]
LR_5 -> LR_7 [label="S(b)"]
LR_5 -> LR_5 [label="S(a)"]
LR_6 -> LR_6 [label="S(b)"]
LR_6 -> LR_5 [label="S(a)"]
LR_7 -> LR_8 [label="S(b)"]
LR_7 -> LR_5 [label="S(a)"]
LR_8 -> LR_6 [label="S(b)"]
LR_8 -> LR_5 [label="S(a)"]
}
Digraph(三)
from graphviz import Digraph
g = Digraph('G', filename='cluster_edge.gv')
g.attr(compound='true')
with g.subgraph(name='cluster0') as c:
c.edges(['ab', 'ac', 'bd', 'cd'])
with g.subgraph(name='cluster1') as c:
c.edges(['eg', 'ef'])
g.edge('b', 'f', lhead='cluster1')
g.edge('d', 'e')
g.edge('c', 'g', ltail='cluster0', lhead='cluster1')
g.edge('c', 'e', ltail='cluster0')
g.edge('d', 'h')
g
Dot代码
digraph G {
compound=true
subgraph cluster0 {
a -> b
a -> c
b -> d
c -> d
}
subgraph cluster1 {
e -> g
e -> f
}
b -> f [lhead=cluster1]
d -> e
c -> g [lhead=cluster1 ltail=cluster0]
c -> e [ltail=cluster0]
d -> h
}
Source
from graphviz import Source
src = Source('digraph "the holy hand grenade" { rankdir=LR; 1 -> 2 -> 3 -> lob }')
src
Dot代码
digraph "the holy hand grenade" {
rankdir=LR;
1 -> 2 -> 3 -> lob
}
结语
粗略地记录了下Graphviz库的一些语法及操作,更多内容可以看Graphviz文档
标签:node,SS,Python,画图,label,Digraph,edge,LR,Graphviz 来源: https://blog.csdn.net/Edisonleeee/article/details/90214609