2014-09-03 128 views
8

我設法正確地產生曲線圖中,但也有一些更多的測試注意以下兩種不同的線路碼的不一致的結果:networkx - 變色根據邊緣屬性/寬度 - 不一致結果

nx.draw_circular(h,edge_color=[h.edge[i][j]['color'] for (i,j) in h.edges_iter()], width=[h.edge[i][j]['width'] for (i,j) in h.edges_iter()]) 

nx.draw_circular(h,edge_color=list(nx.get_edge_attributes(h,'color').values()), width=list(nx.get_edge_attributes(h,'width').values())) 

第一行結果一致的輸出,而第二個產生錯誤的顏色/尺寸每邊的順序。

但是,在我看來,上述兩行都依賴函數調用來返回每個邊的順序的屬性。爲什麼有不同的結果?

使用h[][][]訪問屬性看起來有點笨拙;是否有可能通過點慣例來訪問它,例如, edge.color for edge in h.edges()

或者我錯過了什麼?

回答

13

傳遞給繪圖函數的邊的順序很重要。如果您沒有指定(使用edges關鍵字),您將獲得G.edges()的默認順序。這是最安全的明確給出的參數是這樣的:

import networkx as nx 

G = nx.Graph() 
G.add_edge(1,2,color='r',weight=2) 
G.add_edge(2,3,color='b',weight=4) 
G.add_edge(3,4,color='g',weight=6) 

pos = nx.circular_layout(G) 

edges = G.edges() 
colors = [G[u][v]['color'] for u,v in edges] 
weights = [G[u][v]['weight'] for u,v in edges] 

nx.draw(G, pos, edges=edges, edge_color=colors, width=weights) 

這導致輸出這樣的: enter image description here

+0

tkyou!你的代碼與我的第一行代碼基本相同。但是,你的意思是函數get_edges_attributes不會像邊()那樣返回邊? – timeislove 2014-09-04 01:15:09