2017-07-07 34 views
2

My networkx graph我怎么能在Python

每条边的颜色标签在networkx在我执导networkx图,我已经根据重量绘制边缘的颜色。我可以将它们添加为图中的标签,但它出现两次。我如何与方向

`elarge=[(u,v) for (u,v,d) in G.edges(data=True) if d['weight'] >0.10] 
emed=[(u,v) for (u,v,d) in G.edges(data=True) if d['weight'] >0.05 and d['weight'] <0.10] 
esmall=[(u,v) for (u,v,d) in G.edges(data=True) if d['weight'] <0.05] 

# red_patch = mpatches.Patch(color='red', label= "Probablity < 0.05") 
# plt.legend(handles=[red_patch]) 

# blue_patch = mpatches.Patch(color='blue', label='Probablity > 0.10') 
# plt.legend(handles=[blue_patch]) 

# green_patch = mpatches.Patch(color='green',label='Probablity > 0.15 and Probablity < 0.10') 
# plt.legend(handles=[green_patch]) 


pos=nx.spring_layout(G) # positions for all nodes 
edge_labels = nx.get_edge_attributes(G,'weight') 

# nodes 
nx.draw_networkx_nodes(G,pos,node_size=4000,node_color= "grey") 

# edges 

nx.draw_networkx_edges(G,pos,edgelist=elarge, 
        width=2,alpha = 0.6,edge_color = "b",label = "Probablity > 0.10",arrows=True) 
nx.draw_networkx_edges(G,pos,edgelist=esmall, 
        width=3,alpha=0.6,edge_color='r',label = "Probablity < 0.05",arrows=True) 
nx.draw_networkx_edges(G,pos,edgelist=emed, 
        width=3,alpha=0.6,edge_color='g',label = "'Probablity > 0.15 and Probablity < 0.10'",arrows=True) 

# labels 
nx.draw_networkx_edge_labels(G, pos, labels = edge_labels,arrows=False) 
nx.draw_networkx_labels(G,pos,labels1,font_size=12,font_family='sans-serif') 
plt.legend(numpoints = 1) 
plt.axis('on') 
plt.show() 
+0

请发布代码来生成您的网络,并请将您的网络图像放入实际问题本身。 –

+0

我认为你的担心是标签在图例中出现两次,这是否正确? – Joel

回答

1

要我一起加入边缘标签与不同的颜色只有一次,这看起来像在networkx一个bug(仍然存在于1.11)。在draw_networkx_edges中查看source code,它创建边缘,然后以较粗的线条创建箭头。

创建边缘后,有命令edge_collection.set_label(label),它设置边缘的标签。但创建箭头后,它也有arrow_collection.set_label(label)。因此,边缘和箭头最终会标记在图例中(这就是为什么您会在图例中看到标签以两个关联宽度显示的原因)。

我相信这将足以去您的计算机上的网络x代码,并只需发表评论arrow_collection.set_label(label)。另一种方法是绘制没有箭头的边缘并给它一个标签,然后用箭头重新绘制它(使用相同的颜色),但没有标签。

我已经提交了一个bug report对此,因为我想不出任何理由为什么要为箭头单独标签。