在networkx中基于厚度绘制大型加权网络? [英] Drawing a large weighted network in networkx based on thickness?

查看:381
本文介绍了在networkx中基于厚度绘制大型加权网络?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在网络x中按厚度绘制N个> 1000个节点的加权网络?如果我有一个.csv源,目标节点和每个边缘的权重的列表,并且正在考虑使用以下方法:

How do I draw a weighted network of N>1000 nodes in networkx by thickness? If I have a .csv list of source, target nodes and the weight for each edge, and I am thinking of using the method:

for i in range(N)
G.add_edge(source[i],target[i], weight=weight[i]) 
nx.draw_networkx_edges(G)

但是,我是否必须给每个边缘都加粗?还是每组具有相似厚度的边缘?

but then, do I have to give thickness to every edge? or every group of edges with similar thickness?

推荐答案

您可以分别指定每个边,或者,如果您具有一些计算分组的功能(然后使用多个调用draw_network_edges的功能),则可以按组定义它们.

You can specify each edge individually, or you can define them in groups if you have some function to compute groupings (and then use multiple calls to draw_network_edges).

这是一个带有随机图的示例,该图按原样使用边缘权重,首先定义边缘厚度,其次使用数据作为着色.

Here's an example with a random graph that uses the edge weights as is, first to define the edge thickness and second, using the data as coloring instead.

import matplotlib.pyplot as plt
import networkx as nx
import numpy as np

n = 15; m = 40

# select some edge destinations
L = np.random.choice(xrange(n), 2*m)
# and suppose that each edge has a weight
weights = 0.5 + 5 * np.random.rand(m)

# create a graph object, add n nodes to it, and the edges
G = nx.DiGraph()
G.add_nodes_from(xrange(n))
for i, (fr, to) in enumerate(zip(L[1::2], L[::2])):
  G.add_edge(fr, to, weight=weights[i])

# use one of the edge properties to control line thickness
edgewidth = [ d['weight'] for (u,v,d) in G.edges(data=True)]

# layout
pos = nx.spring_layout(G, iterations=50)
#pos = nx.random_layout(G)

# rendering
plt.figure(1)
plt.subplot(211); plt.axis('off')
nx.draw_networkx_nodes(G, pos)
nx.draw_networkx_edges(G, pos, width=edgewidth,)

plt.subplot(212); plt.axis('off')

# rendering
nx.draw_networkx_nodes(G, pos)
nx.draw_networkx_edges(G, pos, edge_color=edgewidth)

plt.show()

哪个可以给你这样的东西:

Which gives you something like this:

很显然,您也可以使用更复杂的函数来组合适合您的应用程序的边宽值列表(例如合并值或不同属性的乘积).

Clearly you could use a more complicated function to assemble the list of edgewidth values as suitable to your application (e.g. binned values or a product of different properties) as well.

这篇关于在networkx中基于厚度绘制大型加权网络?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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