同一图表上有两个不同的边缘颜色图?

时间:2017-08-16 21:11:23

标签: python graph networkx

我已经完成了以下代码来绘制文件中节点和边缘颜色映射的颜色:

## NODES COLORS##
Active={}
with open('NWWE/node'+str('{:03d}'.format(i))+'.txt', 'r') as f:
    for j in f: 
        a,b=j.split(',')
        Active[a]=b[0] 
for node in G.nodes():
        G.node[node]['category'] = Active[node]
color_map = {'0':'b', '1':'r'}

##EDGES COLOR MAP##
with open('NWWE/edges'+str('{:03d}'.format(i))+'.txt', 'r') as f:
    for k in f: 
        a,b,c=k.split(',')
        G[a][b]['weight']=float(c)

edges,weights = zip(*nx.get_edge_attributes(G,'weight').items())

##DRAW GRAPH##
nx.draw(G, pos, edgelist=edges, edge_color=weights, width=5.0, edge_cmap=plt.cm.Blues, node_color=[color_map[G.node[node]['category']] for node in G])

但是我在节点之间有两种可能的连接,对于每种不同的可能权重,所以我想使用两种不同的边缘颜色图来绘制我的图形。有什么问题吗?

非常感谢!

1 个答案:

答案 0 :(得分:0)

我感谢nx.draw_networkx_edges找到了解决方案:

#one group with one color map :
edges = tuple(list_edgesG1)
weights = tuple([G[e][f]['weight'] for e,f in edges]) 
nx.draw_networkx_edges(G, pos, edgelist=edges, edge_color=weights, width=3.0, edge_cmap=plt.cm.Reds)

#the other group with different color map :
edges2 = tuple(list_edgesG2)
weights2 = tuple([G[e][f]['weight'] for e,f in edges2])
nx.draw_networkx_edges(G , pos, edgelist=edges2, edge_color=weights2, width=3.0, edge_cmap=plt.cm.Blues)