我有一个由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()
ax.invert_yaxis()
方法,可以将0翻转到左上角。节点的位置由pos
设置,您可以按任何方式定义它。例如,使用{i : (i % ncols, i//ncols for i in G.nodes()}
将最大的节点放在(0, 0)附近,而不是(0, 100)。 - tacaswellviridis
颜色方案,但我的matplotlib 1.5无法识别它。如何包含它? - FaCoffee