使用元组在Python中绘制Digraph

时间:2019-11-27 08:54:04

标签: python python-3.x

我想使用元组列表绘制graphviz的Digraph。

from graphviz import Digraph
from itertools import chain   

edges = [("first","sub-first"),("second","sub-second"),("sub-first","sub-second"),("third","sub-third")]
nodes = list(dict.fromkeys([ i for i in chain(*data)]))

您可能会了解,edges是由箭头(x,y)组成的元组列表。 另外,nodes是未重复包装的edges的列表。借助这些数据,我的目的是渲染这样的图形:

enter image description here

1 个答案:

答案 0 :(得分:2)

我不完全确定您遇到了什么问题-首先,不需要定义节点是否具有边缘-您可以将元组的边缘列表传递给graphviz edges函数,该函数接受可迭代:

from graphviz import Digraph

edges = [("first","sub-first"),("second","sub-second"),("sub-first","sub-second"),("third","sub-third")]
graph = Digraph(format='png')
graph.edges(edges)
graph.render()

输出:

enter image description here