在网络x中按重量着色边缘 [英] Colouring edges by weight in networkx

查看:255
本文介绍了在网络x中按重量着色边缘的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只找到类似于我想要的东西:





然而,我似乎无法将此应用于我的问题。我有一个带加权边的图,但是权重并不是唯一的(所以有15个边的权重为1)。我想根据自己的体重对边缘进行着色,重量越轻,颜色越浅。



我尝试应用上述问题中提出的方法,但根据我的理解,这要求权重在每条边上都是唯一的?



到目前为止,我已经按照不同边权重的升序列出了一个列表,并且想要使用这样来分类可能的边缘颜色。我试图避免通过重量来绘制边缘,因为我可能需要在未来绘制一个非常大的图形,边缘上有很大的权重范围。



如果目前还不清楚让我知道在评论中,我会给出更具体的信息。



谢谢!



编辑:
def draw_graph(target):
nlist = [target] + G.neighbors(target)
H = nx.subgraph(G,nlist)
n = H.number_of_edges()
colors = range(n)
labels,weights = colour_and_label_edges(H)

  pos = nx。 spring_layout(H)
nx.draw(H,pos,node_color ='#A0CBE2',edge_color = colors,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)#另存为b
plt.show()#display
$通过

def colour_and_label_edges(graph):
d = {} $ b $ for(u,v)in graph.edges():
d [u,v] =图表[U] [v] ['weight']
temp = []
for d.values()中的val:
如果val不在temp中:
temp.append(val)
weight = sorted(temp,key = int)
return d,权重

上面的代码是不完整的,但想法是这个函数给了我一个权重列表,如下所示:

[1,2,3,4,5,6 ,9,10,16,21,47,89,124,134,224]

然后我想用这个列表为每个权重指定一个颜色,重量越深颜色。 (这个例子相对于数据集使用了一个非常小的子图)。希望将其清除一点:S

解决方案

您可以使用边缘权重和颜色表来绘制它们。

  import matplotlib.pyplot as plt 
导入networkx为nx
进口随机

G = nx.gnp_random_graph(10,0.3)
for g,u,v,d in G.edges(data = True):
d ['weight '] = random.random()

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

pos = nx。 spring_layout(G)
nx.draw(G,pos,node_color ='b',edgelist = edges,edge_color = weights,width = 10.0,edge_cmap = plt.cm.Blues)
plt.savefig( 'edges.png')


I have only found something similar to what I want here:

Coloring networkx edges based on weight

However I can't seem to apply this to my problem. I have a graph with weighted edges, but the weights aren't unique (so there are like 15 edges with weight 1). I want to colour my edges based on the weight they have, the lower the weight the lighter the colour.

I tried to apply the method suggested in the above question, but from what I understand this requires the weights to be unique on each edge?

So far I've produced a list in ascending order of the different edge weights and wanted to use this to classify the possible edge colours. I'm trying to avoid drawing the edges by weight as I may need to draw a very large graph in the future with a huge range of weights on the edges.

If it's unclear let me know in comments and I'll give more specific info.

Thanks!

EDIT: def draw_graph(target): nlist = [target]+G.neighbors(target) H=nx.subgraph(G, nlist) n=H.number_of_edges() colours = range(n) labels,weights = 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

The above code is incomplete, but the idea is the function gives me a list of the weights, as so:

[1, 2, 3, 4, 5, 6, 9, 10, 16, 21, 47, 89, 124, 134, 224]

I then want to use this list to assign each weight a colour, the higher the weight the darker the colour. (I've used a very small subgraph for this example relative to the data set). Hope that clears it up a little :S

解决方案

You can use the edge weights and a colormap to draw them. You might want t a different colormap from the one below.

import matplotlib.pyplot as plt
import networkx as nx
import random

G = nx.gnp_random_graph(10,0.3)
for u,v,d in G.edges(data=True):
    d['weight'] = random.random()

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

pos = nx.spring_layout(G)
nx.draw(G, pos, node_color='b', edgelist=edges, edge_color=weights, width=10.0, edge_cmap=plt.cm.Blues)
plt.savefig('edges.png')

这篇关于在网络x中按重量着色边缘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆