[python] python模塊graphviz使用入門


Graphviz是一款能夠自動排版的流程圖繪圖軟件。python graphviz則是graphviz的python實現。我們可以通過python graphviz實現輕鬆完成各種流程圖的繪製。

1 安裝

該軟件包在Python 2.7和3.5+下運行,請使用pip進行安裝:

pip install graphviz

要渲染生成的可在Graphviz軟件使用DOT源代碼,您還需要安裝Graphviz(下載頁面),並確保包含dot可執行文件的目錄在系統路徑上。

2 快速入門

2.1 基本用法

該graphviz模塊提供了兩個類:Graph和 Digraph。它們分別以DOT語言爲無向圖和有向圖創建圖描述。它們具有相同的 API。通過實例化一個new Graph或 Digraphobject 創建一個圖形:

from graphviz import Digraph

dot = Digraph(comment='The Round Table')

print(dot)

輸出如下信息

// The Round Table
digraph {
}

然後我們可以添加點和邊,通過node()和edge()或edges()來實現。

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')
print(dot.source)  

生成的源代碼如下:

// 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]
}

最後我們可以通過如下代碼保存圖像pdf文件,並顯示。通過設置view=True將自動使用系統默認的文件類型的查看器應用程序打開生成的文件(PDF,PNG,SVG等)。

dot.render('test-output/round-table.gv', view=True)  

在這裏插入圖片描述

2.2 輸出圖像格式

要使用與默認PDF 不同的輸出文件格式,請format在創建Graph或 Digraph對象時使用參數:

from graphviz import Graph

g = Graph(format='png')

或者在基本用法的例子中在輸出中添加format='jpg’便可以獲得jpg圖像。

dot.render('test-output/round-table.gv',format='jpg', view=True)  

如果是想設置輸出圖像的dpi,需要在創建Graph或Digraph對象時,設置dpi參數。

from graphviz import Graph

g = Graph(format='png')
g.graph_attr['dpi'] = '300'

2.3 圖像style設置

使用graph_attr,node_attr和 edge_attr參數更改默認外觀的圖表,點和連接線。

from graphviz import Digraph

ps = Digraph(name='pet-shop', node_attr={'shape': 'plaintext'},format='png')
ps.node('parrot')
ps.node('dead')
ps.edge('parrot', 'dead')

在這裏插入圖片描述

2.4 屬性

要設置圖中的所有後續圖形,點或邊的樹形,請使用attr()方法,如下所示:

from graphviz import Digraph
from graphviz import Graph
ni = Graph('ni',format='jpg')

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.attr(rankdir='LR')

ni.edges(['12', '23', '34', '45'])
print(ni.source) 
ni.view()

在這裏插入圖片描述

2.5 子圖和聚類

圖和有向圖對象有一個subgraph()-用於向實例添加子圖的方法。
有兩種方法可以使用它:使用與唯一參數(其內容作爲子圖添加)類型相同的現成圖形對象,或者省略圖形參數(返回上下文管理器,以便在with塊中更優雅地定義子圖內容)。
第一個用法選項,只有graph作爲參數:

from graphviz import Digraph
from graphviz import Graph
p = Graph(name='parent', node_attr={'shape': 'plaintext'},format='png')
p.edge('spam', 'eggs')

c = Graph(name='child', node_attr={'shape': 'box'})
c.edge('foo', 'bar')

p.subgraph(c)
p.view()

第二次使用,帶有with-block(忽略graph參數):

p = Graph(name='parent')
p.edge('spam', 'eggs')

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

兩者結果相同如下圖所示:
在這裏插入圖片描述

3 實例

代表的實例圖像如下所示

  1. 有向圖
    代碼
from graphviz import Digraph

g = Digraph('G', filename='hello.gv',format='png')

g.edge('Hello', 'World')

g.view()

結果如圖所示:

在這裏插入圖片描述

  1. 無向圖
    代碼
from graphviz import Graph

g = Graph('G', filename='process.gv', engine='sfdp',format='png')

g.edge('run', 'intr')
g.edge('intr', 'runbl')
g.edge('runbl', 'run')
g.edge('run', 'kernel')
g.edge('kernel', 'zombie')
g.edge('kernel', 'sleep')
g.edge('kernel', 'runmem')
g.edge('sleep', 'swap')
g.edge('swap', 'runswap')
g.edge('runswap', 'new')
g.edge('runswap', 'runmem')
g.edge('new', 'runmem')
g.edge('sleep', 'runmem')

g.view()

結果如圖所示:
在這裏插入圖片描述

  1. 子圖
    代碼

from graphviz import Digraph

g = Digraph('G', filename='cluster.gv',format='png')

# NOTE: the subgraph name needs to begin with 'cluster' (all lowercase)
#       so that Graphviz recognizes it as a special cluster subgraph

with g.subgraph(name='cluster_0') as c:
    c.attr(style='filled', color='lightgrey')
    c.node_attr.update(style='filled', color='white')
    c.edges([('a0', 'a1'), ('a1', 'a2'), ('a2', 'a3')])
    c.attr(label='process #1')

with g.subgraph(name='cluster_1') as c:
    c.attr(color='blue')
    c.node_attr['style'] = 'filled'
    c.edges([('b0', 'b1'), ('b1', 'b2'), ('b2', 'b3')])
    c.attr(label='process #2')

g.edge('start', 'a0')
g.edge('start', 'b0')
g.edge('a1', 'b3')
g.edge('b2', 'a3')
g.edge('a3', 'a0')
g.edge('a3', 'end')
g.edge('b3', 'end')

g.node('start', shape='Mdiamond')
g.node('end', shape='Msquare')

g.view()

結果如圖所示:
在這裏插入圖片描述

4 如何進一步使用python graphviz

python graphviz官方文檔如下:

https://graphviz.readthedocs.io/en/stable/index.html

在實際使用時,參考官方實例就行。但是python graphviz文檔介紹不全,很多graphviz軟件參數使用沒有說清楚。如果不會graphviz語法,無法很好地使用python graphviz。一些python graphviz特性可以參考文檔,然後對照使用。

Graphviz 畫圖的一些總結
Graphviz入門
GraphViz DOT有向圖 (一)元素說明

實際上graphviz畫一些流程圖即可,而且需要較大的調整參數。因此如果非緊急繪圖建議使用visio。

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