2014-09-03 78 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