如何在有向图上显示边标签 draw_networkx
How to show edge labes on DiGraph draw_networkx
我有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'),但是一个标签叠加在另一个标签上,我们只看到一个(只是range
= 10,但是有range
= 20)。如何解决?
您可以尝试将 draw_networkx_edge_labels 中的 label_pos
属性设置为 0.5(边缘中间)以外的值,例如 0.3
我有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'),但是一个标签叠加在另一个标签上,我们只看到一个(只是range
= 10,但是有range
= 20)。如何解决?
您可以尝试将 draw_networkx_edge_labels 中的 label_pos
属性设置为 0.5(边缘中间)以外的值,例如 0.3