2017-07-07 65 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對此,因爲我想不出任何理由爲什麼要爲箭頭單獨標籤。