使用元组在 Python 中绘制有向图

Use tuples to draw Digraph in Python

我想用元组列表绘制graphviz的有向图

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。有了这些数据,我的目标是呈现这样的图表:

我不完全确定你遇到了什么问题 - 首先你不需要定义节点,如果它们有边 - 你可以将元组的边列表传递给 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()

输出: