【发布时间】:2016-06-17 08:58:08
【问题描述】:
我有一个由10000 节点组成的无标度网络,但边缘的纹理和节点的数量使其过于复杂而无法理解。 我希望能够直观地定位连接度最高的节点。
如何根据 k 度为节点着色? 具体来说,我想根据预先分配的范围为它们着色,例如:
- 如果
1<k<10,则为绿色; - 淡蓝色如果
11<k<20; - 蓝色如果
21<k<30; - 如果
31<k<40,则为紫色; - ...
这是我获取网络的方法:
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()
【问题讨论】:
标签: python matplotlib colors networkx