Python中模塊graphviz使用入門(mén)
graphviz 是一個(gè)用于創(chuàng)建和操作圖形(如流程圖、網(wǎng)絡(luò)圖等)的 Python 庫(kù)。它依賴于 Graphviz 軟件包,后者是一個(gè)開(kāi)源的圖形可視化軟件。graphviz 庫(kù)允許你從 Python 腳本中生成 Graphviz 的點(diǎn)文件(DOT 文件),并渲染成圖像。
以下是使用 graphviz 的基本步驟:
1.安裝
1.1 安裝 Graphviz 軟件首先,你需要在你的系統(tǒng)上安裝 Graphviz 軟件包。你可以從 Graphviz 官網(wǎng) 下載并安裝它。安裝完成后,確保 dot 命令在你的系統(tǒng)路徑中可用。
1.2 安裝 Python 的 graphviz 庫(kù)你可以使用 pip 來(lái)安裝 Python 的 graphviz 庫(kù):
pip install graphviz
2. 基本用法
該graphviz模塊提供了兩個(gè)類(lèi):Graph和 Digraph。它們分別以DOT語(yǔ)言為無(wú)向圖和有向圖創(chuàng)建圖描述。它們具有相同的 API。通過(guò)實(shí)例化一個(gè)new Graph或 Digraphobject 創(chuàng)建一個(gè)圖形:
from graphviz import Digraph dot = Digraph(comment='The Round Table') print(dot)
輸出如下信息
// The Round Table
digraph {
}
然后我們可以添加點(diǎn)和邊,通過(guò)node()和edge()或edges()來(lái)實(shí)現(xiàn)。
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] }
最后我們可以通過(guò)如下代碼保存圖像pdf文件,并顯示。通過(guò)設(shè)置view=True將自動(dòng)使用系統(tǒng)默認(rèn)的文件類(lèi)型的查看器應(yīng)用程序打開(kāi)生成的文件(PDF,PNG,SVG等)。
dot.render('test-output/round-table.gv', view=True)
2.1 輸出圖像格式
要使用與默認(rèn)PDF 不同的輸出文件格式,請(qǐng)format在創(chuàng)建Graph或 Digraph對(duì)象時(shí)使用參數(shù):
from graphviz import Graph g = Graph(format='png')
或者在基本用法的例子中在輸出中添加format='jpg’便可以獲得jpg圖像。
dot.render('test-output/round-table.gv',format='jpg', view=True)
如果是想設(shè)置輸出圖像的dpi,需要在創(chuàng)建Graph或Digraph對(duì)象時(shí),設(shè)置dpi參數(shù)。
from graphviz import Graph g = Graph(format='png') g.graph_attr['dpi'] = '300'
2.2 圖像style設(shè)置
使用graph_attr,node_attr和 edge_attr參數(shù)更改默認(rèn)外觀的圖表,點(diǎn)和連接線。
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.3 屬性
要設(shè)置圖中的所有后續(xù)圖形,點(diǎn)或邊的樹(shù)形,請(qǐng)使用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.4 子圖和聚類(lèi)
圖和有向圖對(duì)象有一個(gè)subgraph()-用于向?qū)嵗砑幼訄D的方法。
有兩種方法可以使用它:使用與唯一參數(shù)(其內(nèi)容作為子圖添加)類(lèi)型相同的現(xiàn)成圖形對(duì)象,或者省略圖形參數(shù)(返回上下文管理器,以便在with塊中更優(yōu)雅地定義子圖內(nèi)容)。
第一個(gè)用法選項(xiàng),只有g(shù)raph作為參數(shù):
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參數(shù)):
p = Graph(name='parent') p.edge('spam', 'eggs') with p.subgraph(name='child', node_attr={'shape': 'box'}) as c: c.edge('foo', 'bar')
兩者結(jié)果相同如下圖所示:
3 實(shí)例
代表的實(shí)例圖像如下所示
- 有向圖
代碼
from graphviz import Digraph g = Digraph('G', filename='hello.gv',format='png') g.edge('Hello', 'World') g.view()
結(jié)果如圖所示:
- 無(wú)向圖
代碼
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()
結(jié)果如圖所示:
- 子圖
代碼
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()
結(jié)果如圖所示:
4 如何進(jìn)一步使用python graphviz
python graphviz官方文檔如下:
在實(shí)際使用時(shí),參考官方實(shí)例就行。
實(shí)際上graphviz畫(huà)一些流程圖即可,而且需要較大的調(diào)整參數(shù)。因此如果非緊急繪圖建議使用visio。
到此這篇關(guān)于Python中模塊graphviz使用入門(mén)的文章就介紹到這了,更多相關(guān)Python 模塊graphviz使用內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
python知識(shí):裝飾器@property到底有啥用途
這篇文章主要介紹了python裝飾器@property到底有啥用途,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-01-01Pandas?DataFrame列快速轉(zhuǎn)換為列表(3秒學(xué)會(huì)!)
這篇文章主要給大家介紹了關(guān)于Pandas?DataFrame列如何快速轉(zhuǎn)換為列表的相關(guān)資料,在Python的pandas庫(kù)中可以使用DataFrame的tolist()方法將DataFrame轉(zhuǎn)化為列表,需要的朋友可以參考下2023-10-10Django rstful登陸認(rèn)證并檢查session是否過(guò)期代碼實(shí)例
這篇文章主要介紹了Django rstful登陸認(rèn)證并檢查session是否過(guò)期代碼實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-08-08詳解python環(huán)境安裝selenium和手動(dòng)下載安裝selenium的方法
這篇文章主要介紹了詳解python環(huán)境安裝selenium和手動(dòng)下載安裝selenium的方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-03-03python實(shí)現(xiàn)根據(jù)月份和日期得到星座的方法
這篇文章主要介紹了python實(shí)現(xiàn)根據(jù)月份和日期得到星座的方法,涉及Python操作字符串及數(shù)組的技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-03-03Python實(shí)現(xiàn)外星人去哪了小游戲詳細(xì)代碼
今天為大家?guī)?lái)一款小游戲,名叫外星人去哪了,用Python語(yǔ)言實(shí)現(xiàn)完成,代碼簡(jiǎn)潔易懂,感興趣的小伙伴快來(lái)看看吧2022-03-03使用Python寫(xiě)一個(gè)量化股票提醒系統(tǒng)
這篇文章主要介紹了小白用Python寫(xiě)了一個(gè)股票提醒系統(tǒng),迷你版量化系統(tǒng),完美的實(shí)現(xiàn)了實(shí)時(shí)提醒功能,代碼簡(jiǎn)單易懂,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-08-08Python應(yīng)用之利用pyecharts畫(huà)中國(guó)地圖
這篇文章主要介紹了Python應(yīng)用之利用pyecharts畫(huà)中國(guó)地圖,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-07-07