我想使用元组列表绘制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
的列表。借助这些数据,我的目的是渲染这样的图形:
答案 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()
输出: