我只发现了类似于我想要的东西:
但是,我似乎无法将此应用于我的问题.我有一个带加权边的图,但权重不是唯一的(所以有15个边有权重1).我想根据它们的重量为我的边缘着色,重量越低,颜色越浅.
我尝试应用上述问题中建议的方法,但据我所知,这要求权重在每个边缘都是唯一的?
到目前为止,我已经按照不同边缘权重的升序生成了一个列表,并希望使用它来对可能的边缘颜色进行分类.我试图避免按重量绘制边缘,因为我可能需要绘制一个非常大的图形,边缘上有很大的权重范围.
如果不清楚,请在评论中告诉我,我会提供更具体的信息.
谢谢!
编辑:def draw_graph(目标):nlist = [目标] + G.邻居(目标)H = nx.subgraph(G,nlist)n = H.number_of_edges()颜色=范围(n)标签,权重= colour_and_label_edges(H )
pos = nx.spring_layout(H)
nx.draw(H, pos, node_color='#A0CBE2',edge_color=colours, node_size=100, edge_cmap=plt.cm.Blues, width=0.5, with_labels=False)
nx.draw_networkx_edge_labels(H, pos, edge_labels=labels)
plt.savefig("Graphs/edge_colormap_%s.png" % target) # save as png
plt.show() # display
pass
def colour_and_label_edges(graph):
d={}
for (u,v) in graph.edges():
d[u,v]=graph[u][v]['weight']
temp=[]
for val in d.values():
if val not in temp:
temp.append(val)
weights = sorted(temp,key=int)
return d, weights
Run Code Online (Sandbox Code Playgroud)
上面的代码是不完整的,但想法是函数给我一个权重列表,如下:
[1,2,3,4,5,6,9,10,16,21,47,89,124,134,224]
然后,我想使用此列表为每个重量分配颜色,重量越高颜色越深.(我在这个例子中使用了一个非常小的子图,相对于数据集).希望能稍微清理一下:S
我有一个代表网络拓扑结构的关键节点是颜色的networkx春天布局为红色,其余是蓝色边缘与虚线表示的关键节点的路由我怎么可以在指定的时间间隔动画绘制networkx边缘?
#!/usr/bin/env python
import matplotlib.pyplot as plt
import networkx as nx
import matplotlib as mpl
G=nx.Graph()
G.add_edge('a','b',weight=0.6)
G.add_edge('a','c',weight=0.2)
G.add_edge('c','d',weight=0.1)
G.add_edge('c','e',weight=0.7)
G.add_edge('c','f',weight=0.9)
G.add_edge('a','d',weight=0.3)
elarge=[(u,v) for (u,v,d) in G.edges(data=True) if d['weight'] >0.5]
esmall=[(u,v) for (u,v,d) in G.edges(data=True) if d['weight'] <=0.5]
#specify an initial position that is not random e.g. use a circular layout
pos=nx.circular_layout(G)
pos=nx.spring_layout(G,dim=2,pos=pos) # positions for all nodes
#pos=nx.spring_layout(G) # positions for all nodes
print "Graph xy positions"
print pos
# nodes
nx.draw_networkx_nodes(G,pos,node_size=700)
# edges
nx.draw_networkx_edges(G,pos,edgelist=elarge,
width=6)
nx.draw_networkx_edges(G,pos,edgelist=esmall,
width=6,alpha=0.5,edge_color='b',style='dashed')
# …Run Code Online (Sandbox Code Playgroud) 我创建了一个MultiDiGraph,networkx尝试在其中添加权重到边缘,然后根据边缘出现的频率/次数分配新的权重。我使用以下代码创建图形并添加权重,但是我不确定如何处理基于计数的权重分配:
g = nx.MultiDiGraph()
df = pd.read_csv('G:\cluster_centroids.csv', delimiter=',')
df['pos'] = list(zip(df.longitude,df.latitude))
dict_pos = dict(zip(df.cluster_label,df.pos))
#print dict_pos
for row in csv.reader(open('G:\edges.csv', 'r')):
if '[' in row[1]: #
g.add_edges_from(eval(row[1]))
for u, v, d in g.edges(data=True):
d['weight'] = 1
for u,v,d in g.edges(data=True):
print u,v,d
Run Code Online (Sandbox Code Playgroud)
编辑
我能够通过以下操作为原始问题的第一部分成功分配权重:
for u, v, d in g.edges(data=True):
d['weight'] = 1
for u,v,d in g.edges(data=True):
print u,v,d
Run Code Online (Sandbox Code Playgroud)
但是,我仍然无法根据出现一条边的次数重新分配权重(图形中的一条边可以多次出现)?我需要完成此操作,以可视化计数更高的边缘与计数较低的边缘(使用边缘颜色或宽度)。我不确定如何根据计数重新分配权重,请告知。以下是示例数据,以及指向我完整数据集的链接。
数据
样本质心(节点):
cluster_label,latitude,longitude
0,39.18193382,-77.51885109
1,39.18,-77.27
2,39.17917928,-76.6688633
3,39.1782,-77.2617
4,39.1765,-77.1927
5,39.1762375,-76.8675441
6,39.17468,-76.8204499
7,39.17457332,-77.2807235
8,39.17406072,-77.274685
9,39.1731621,-77.2716502
10,39.17,-77.27
Run Code Online (Sandbox Code Playgroud)
样本边缘:
user_id,edges
11011,"[[340, 269], …Run Code Online (Sandbox Code Playgroud)