我有一個由10000
節點構成的無標度網絡,但是邊緣的紋理和節點的數量使得它非常複雜而難以理解。 我希望能夠直觀地找到最高度連接的節點。Python:如何根據其程度對網絡的節點着色?
如何根據度數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()
這是沒有應用差分着色結果。 PS: ID爲0的初始節點位於左上角。
請檢查我的編輯圖像。我一直有這個問題。在我的網絡中,ID爲「0」的節點在左上角,而在你的左下角。這會影響「熱圖」的顯示方式,因爲該網絡是通過從上一行開始的* growth *和* preferential attachment *生成的。如何改變你的情節? – FaCoffee
有一個'ax.invert_yaxis()'方法會將0翻轉到左上方。節點放置位置由'pos'設置,您可以根據需要定義它。例如,使用'{i:(i%ncols,i // ncols for i in G.nodes()}'把最大的節點放在(0,0)附近而不是(0,100)。 – tacaswell
我喜歡' viridis'配色方案,但我的matplotlib 1.5無法識別它。如何包含它? – FaCoffee