Python 2.7 用python中的graphviz从点文件绘制有向图
这是Python 2.7 用python中的graphviz从点文件绘制有向图,python-2.7,graph,graphviz,dot,Python 2.7,Graph,Graphviz,Dot,这是graphviz的示例。我找不到任何从现有的dot源文件生成有向图的方法。方法,如render和view保存在新文件中 如何显示现有点代码中的图形?我可以使用源类解决它 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
graphviz
的示例。我找不到任何从现有的dot
源文件生成有向图的方法。方法,如render
和view
保存在新文件中
如何显示现有
点代码中的图形?我可以使用源类解决它
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-pip-install-graphviz
,否则它将无法工作。您可以使用中定义的源.from_文件('/path/to/dot_文件')
函数
因此,守则将是:
from graphviz import Source
path = '/path/to/dot_file'
s = Source.from_file(path)
s.view()
我编写了一个非常简短的代码,用于在Python中使用点文件显示图形
代码如下所示:
来自graphviz导入源
路径='abcd.dot'
s=源。从_文件(路径)
印刷品(s.来源)
s、 渲染('abcd.gv',format='jpg',view=True)
python3.7/tmp/graphviz.py回溯(最后一次调用):文件“/tmp/graphviz.py”,第3行,从graphviz导入源文件“/tmp/graphviz.py”,第3行,从graphviz导入源导入错误:无法从“graphviz”导入名称“Source”(/tmp/graphviz.py)