簡體   English   中英

從 DOT 文件在 python 中使用 graphviz 繪制有向圖

[英]Plotting the Digraph with graphviz in python from DOT file

這是graphvizAPI 參考 我找不到任何從現有dot源文件生成有向圖的方法。 renderview等方法保存在新文件中。

如何從現有的dot代碼顯示圖形?

我能夠使用Source類來解決它。

from graphviz import Source
temp = """
digraph G{
edge [dir=forward]
node [shape=plaintext]

0 [label="0 (None)"]
0 -> 5 [label="root"]
1 [label="1 (Hello)"]
2 [label="2 (how)"]
2 -> 1 [label="advmod"]
3 [label="3 (are)"]
4 [label="4 (you)"]
5 [label="5 (doing)"]
5 -> 3 [label="aux"]
5 -> 2 [label="advmod"]
5 -> 4 [label="nsubj"]
}
"""
s = Source(temp, filename="test.gv", format="png")
s.view()

輸出

輸出將在同一文件夾中,並且可以更改格式。

PS - 在 Ubuntu 上安裝graphviz 首先使用sudo apt install graphviz進行sudo apt install graphviz ,然后使用sudo pip install graphviz ,否則將無法工作。

您可以使用API 中定義的Source.from_file('/path/to/dot_file')函數。

因此代碼將是:

from graphviz import Source
path = '/path/to/dot_file'
s = Source.from_file(path)
s.view()

我在 Python 中使用點文件顯示了一個非常短的代碼。

代碼是這樣的:

from graphviz import Source

path = 'abcd.dot'
s = Source.from_file(path)
print(s.source)

s.render('abcd.gv', format='jpg',view=True)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM