Python:如何根据网络的程度为网络的节点着色? [英] Python: how to color the nodes of a network according to their degree?

查看:86
本文介绍了Python:如何根据网络的程度为网络的节点着色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个由 10000 个节点组成的无标度网络,但是边缘的纹理和节点的数量使它变得太复杂而无法理解.我希望能够直观地找到连接最紧密的节点.

如何根据节点的度k为节点着色?具体来说,我想根据预先指定的范围为节点着色,例如:

  • 如果 1< k< 10 ,则为绿色;
  • 如果 11< k< 20 ;
  • ,则为浅蓝色
  • 如果 21< k< 30 ;
  • 如果 31< k< 40 ;
  • ...

这是我获取网络的方式:

 将networkx导入为nx导入matplotlib.pyplot作为pltn = 10000#节点数m = 3#初始链接数种子= 500G = nx.barabasi_albert_graph(n,m,种子)ncols = 100pos = {i:G.nodes()中i的(i%ncols,(n-i-1)//ncols)}无花果,ax = plt.subplots()nx.draw(G,pos,with_labels = False,ax = ax,node_size = 10)degree = G.degree()#Dict,带有节点ID,度sum_of_degrees =总和(degrees.values())#度的总和avg_degree_unaltered = sum_of_degrees/10000#平均度< k>short_path = nx.average_shortest_path_length(G)print('seed:'+ str(seed)+',短路径:'+ str(round(short_path,3))+',log(N)= 4')#绘制图表plt.xlim(-20,120,10)plt.xticks(numpy.arange(-20,130,20.0))ylim(120,-20,10)plt.yticks(numpy.arange(-20,130,20.0))plt.axis('on')title_string =('无标度网络')subtitle_string =('100x100'+'='+ str(n)+'节点')plt.suptitle(title_string,y = 0.99,fontsize = 17)plt.title(subtitle_string,fontsize = 8)plt.show() 

这是不应用差异着色的结果. PS: ID为0的初始节点位于左上角.

解决方案

在后台,这只是实现为matplotlib scatter 图,networkx API允许您传递许多

这会根据程度缩放颜色和大小.

这可以使用 BoundryNorm 和离散的颜色映射表进行扩展,以将节点划分为带.

I have a scale-free network made of 10000 nodes, but the texture of edges and the number of nodes make it too intricate to be made sense of. I want to be able to visually locate the most highly connected nodes.

How could I color the nodes based on their degree k? Specifically, I would like to color them based on pre-assigned ranges, such as:

  • Green if 1<k<10;
  • Light blue if 11<k<20;
  • Blue if 21<k<30;
  • Purple if 31<k<40;
  • ...

Here is how I obtain the network:

import networkx as nx
import matplotlib.pyplot as plt
n = 10000  # Number of nodes
m = 3  # Number of initial links
seed = 500
G = nx.barabasi_albert_graph(n, m, seed)
ncols = 100
pos = {i : (i % ncols, (n-i-1)//ncols) for i in G.nodes()}
fig, ax = plt.subplots()
nx.draw(G, pos, with_labels=False, ax=ax, node_size=10)
degrees=G.degree() #Dict with Node ID, Degree
sum_of_degrees=sum(degrees.values()) #Sum of degrees
avg_degree_unaltered=sum_of_degrees/10000 #The average degree <k>
short_path=nx.average_shortest_path_length(G)
print('seed: '+str(seed)+', short path: '+str(round(short_path,3))+', log(N)=4')
#Plot the graph
plt.xlim(-20,120,10)
plt.xticks(numpy.arange(-20, 130, 20.0))
plt.ylim(120,-20,10) 
plt.yticks(numpy.arange(-20, 130, 20.0))
plt.axis('on')
title_string=('Scale-Free Network') 
subtitle_string=('100x100'+' = '+str(n)+' nodes')
plt.suptitle(title_string, y=0.99, fontsize=17)
plt.title(subtitle_string, fontsize=8)
plt.show()

This is the result without applying the differential coloring. PS: the initial node with ID 0 is in the top left corner.

解决方案

Under the hood this is just implemented as a matplotlib scatter plot and the networkx API lets you pass many options through

import numpy as np
import matplotlib.colors as mcolors
import networkx as nx
import matplotlib.pyplot as plt
n = 10000  # Number of nodes
m = 3  # Number of initial links
seed = 500
G = nx.barabasi_albert_graph(n, m, seed)

ncols = 100
pos = {i : (i % ncols, (n-i-1)//ncols) for i in G.nodes()}

fig, ax = plt.subplots()
degrees = G.degree() #Dict with Node ID, Degree
nodes = G.nodes()
n_color = np.asarray([degrees[n] for n in nodes])
sc = nx.draw_networkx_nodes(G, pos, nodelist=nodes, node_color=n_color, cmap='viridis',
                            with_labels=False, ax=ax, node_size=n_color)
# use a log-norm, do not see how to pass this through nx API
# just set it after-the-fact
sc.set_norm(mcolors.LogNorm())
fig.colorbar(sc)

This scales both the color and the size based on the degree.

This can be extended using BoundryNorm and a discrete color map to segment the nodes into bands.

这篇关于Python:如何根据网络的程度为网络的节点着色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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