如何在DiGraph draw_networkx上显示边缘标签

时间:2018-08-25 11:42:14

标签: python graph networkx digraphs

我有nx.DiGraph这样的人(玩具示例)

import networkx as nx
G = nx.DiGraph()
G.add_node('a', weight= 1)
G.add_node('b', weight= 2)
G.add_node('c', weight= 3)
G.add_edge('a', 'b', range = 10)
G.add_edge( 'b','a', range = 20)
G.add_edge('a', 'c', range = 10)
G.add_edge( 'c','a', range = 20)
G.add_edge('c', 'b', range = 10)
G.add_edge( 'b','c', range = 20)

然后我像波纹管一样抽奖

plt.figure(figsize=(5,5))
edge_labels = nx.get_edge_attributes(G,'range')
edge_size= [edge_labels[v] for v in edge_labels]
node_labels = nx.get_node_attributes(G,'weight') 
node_size = [node_labels[v] for v in node_labels]
pos = nx.spring_layout(G) # circularspring_layout, scale=1., center=None
nx.draw_networkx_edge_labels(G,pos, edge_labels = edge_labels, font_size=25, font_color='k')
nx.draw_networkx(G, pos,  node_size=[d['weight']*2000 for _, d in G.nodes(data=True)], edge_color='.4', 
                 node_color= [d['weight'] for _, d in G.nodes(data=True)], alpha=0.6,with_labels=True,  width = [d['range'] for _, _, d in G.edges(data=True)],
                cmap=plt.cm.seismic, font_size =30, arrows = False) # 
plt.axis('off')
plt.tight_layout()
plt.show()

我想在绘制上显示边缘标签(边缘属性“范围”),但是一个标签叠加在另一个标签上,我们只能看到一个标签(仅range = 10,但是有{{1} } = 20)。如何解决? enter image description here

1 个答案:

答案 0 :(得分:0)

您可以尝试将draw_networkx_edge_labels中的label_pos属性设置为0.5(边缘的中间)以外的值,例如0.3