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文檔

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章