简体   繁体   English

使用networkx绘制平行边

[英]draw parallel edges using networkx

Below is a very simple example 下面是一个非常简单的示例

import networkx as nx
import matplotlib.pyplot as plt

g = nx.DiGraph()
g.add_nodes_from([1,2,3])
g.add_edge(1,2, weight = 1)
g.add_edge(1,3, weight = 1)
g.add_edge(2,1, weight = 2)
nx.draw(g,with_labels=True)
plt.draw()
plt.show()

I would like to add the weight label to the edge and show parallel edges for 1,2 and 2,1 how do I do this. 我想将权重标签添加到边缘,并显示1,2和2,1的平行边缘,我该怎么做。 I am using jupyter notebook. 我正在使用jupyter笔记本。

many thanks! 非常感谢!

Here is a method to add weight labels to the edges: 这是在边缘添加权重标签的方法:

pos=nx.spring_layout(g)
nx.draw_networkx_nodes(g, pos)
nx.draw_networkx_labels(g, pos)
nx.draw_networkx_edges(g,pos)
nx.draw_networkx_edge_labels(g,pos,edge_labels={x:g[x[0]][x[1]]['weight'] for x in g.edges})
plt.axis('off')
plt.show()

Output: 输出:

图形

Unfortunately, I don't think that networkx (or more precisely: matplotlib) is able to handle parallel edges drawing. 不幸的是,我认为networkx(或更确切地说是matplotlib)不能处理平行边图。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM