gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 21:54:28 24 4
gpt4 key购买 nike

我想使用元组列表绘制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) 组成的元组列表。另外,节点是解压缩的列表,没有重复项。有了这些数据,我的目标是呈现这样的图表:

enter image description here

最佳答案

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

关于python - 在Python中使用元组绘制有向图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59066379/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com