gpt4 book ai didi

python - 在 Python 3.x 中使用 matplotlib 和 Networkx 的曲线边缘

转载 作者:行者123 更新时间:2023-12-04 10:29:54 37 4
gpt4 key购买 nike

我想使用 Networkx 框架和 matplotlib 绘制曲线边缘。

基本上与以下链接相同的问题:

Networkx: Overlapping edges when visualizing MultiGraph

一个答案是:

import networkx as nx
G = nx.DiGraph()
G.add_nodes_from([0,1])
pos = nx.circular_layout(G)
nx.draw_networkx_nodes(G, pos, connectionstyle='arc3, rad = 0.1', node_color = 'r', node_size = 100, alpha = 1)
nx.draw_networkx_edges(G, pos,connectionstyle='arc3, rad = 0.1', edgelist = [(0,1)], width = 2, alpha = 0.5, edge_color='b')
nx.draw_networkx_edges(G, pos,connectionstyle='arc3, rad = 0.1', edgelist= [(1,0)], width = 1, alpha = 1)
plt.axis('off')
plt.show()

但这会产生:

enter image description here

最后我想制作这样的东西:

enter image description here

最佳答案

我认为你不能直接用 networkx 函数来做到这一点。但是您可以使用您计算出的节点位置直接使用 matplotlib。

例如。基于 this :

import networkx as nx
G = nx.DiGraph()
G.add_nodes_from([0,1])
pos = nx.circular_layout(G)
nx.draw_networkx_nodes(G, pos, node_color = 'r', node_size = 100, alpha = 1)
ax = plt.gca()
ax.annotate("",
xy=pos[0], xycoords='data',
xytext=pos[1], textcoords='data',
arrowprops=dict(arrowstyle="->", color="0.5",
shrinkA=5, shrinkB=5,
patchA=None, patchB=None,
connectionstyle="arc3,rad=0.3",
),
)
plt.axis('off')
plt.show()

给出:

enter image description here

关于python - 在 Python 3.x 中使用 matplotlib 和 Networkx 的曲线边缘,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60464285/

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