gpt4 book ai didi

networkx - 如何在 Networkx/Graphviz 中绘制平行边

转载 作者:行者123 更新时间:2023-12-04 21:10:50 34 4
gpt4 key购买 nike

我正在尝试使用 NetworkX 在两个节点之间添加平行边,但由于以下错误而失败。我究竟做错了什么?

import networkx as nx
import graphviz

g1 = nx.MultiGraph()

node1 = 'a'
node2 = 'b'

g1.add_edge(node1,node2,key='one')
g1.add_edge(node1,node2,key='two')

A = nx.to_agraph(g1)
A.add_subgraph()

A.draw('test2.png', prog='dot')

错误:
Traceback (most recent call last):
File "test2.py", line 12, in <module>
A = nx.to_agraph(g1)
File "C:\python27\lib\site-packages\networkx-1.11rc1-py2.7.egg\networkx\drawing\nx_agraph.py", line 152, in to_agraph
A.add_edge(u,v,key=str(key),**str_edgedata)
File "C:\python27\lib\site-packages\pygraphviz\agraph.py", line 481, in add_edge
eh = gv.agedge(self.handle, uh, vh, key, _Action.find)
KeyError: 'agedge: no key'

最佳答案

您可以在不使用 graphviz 的情况下执行相同操作。我这样做添加 connectionstyle到 nx.draw:

import networkx as nx

g1 = nx.DiGraph(directed=True)

node1 = 'a'
node2 = 'b'

g1.add_edge(node1,node2,key=1)
g1.add_edge(node2,node1,key=2)

nx.draw(g1, with_labels=True, arrows = True, connectionstyle='arc3, rad = 0.1')

See here the result

关于networkx - 如何在 Networkx/Graphviz 中绘制平行边,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35007046/

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