两张不同的边缘颜色图在同一张图上?
问题描述:
我也做了下面的代码从文件中绘制的节点和边缘的彩色地图颜色:两张不同的边缘颜色图在同一张图上?
## 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])
但我有两种节点之间的可能联系的,与各种不同的可能的权重,所以我想绘制我的图形使用两个不同的颜色地图的边缘。有什么问题吗?
非常感谢!
答
我找到解决方案感谢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)