我经常遇到复杂网络中的长尾度分布/直方图,如下图所示。它们使这些尾巴的重尾,嗯,非常沉重和拥挤许多观察:
然而,我读过的许多出版物都有更清晰的度数分布,在分布结束时没有这种结论,观察的间隔更均匀。
!
如何使用NetworkX
和matplotlib
创建这样的图表?
答案 0 :(得分:12)
使用log binning(see also)。下面是一个代码,用于获取表示度数值直方图的Counter
对象,并对分布进行记录,以产生更稀疏,更平滑的分布。
import numpy as np
def drop_zeros(a_list):
return [i for i in a_list if i>0]
def log_binning(counter_dict,bin_count=35):
max_x = log10(max(counter_dict.keys()))
max_y = log10(max(counter_dict.values()))
max_base = max([max_x,max_y])
min_x = log10(min(drop_zeros(counter_dict.keys())))
bins = np.logspace(min_x,max_base,num=bin_count)
# Based off of: http://stackoverflow.com/questions/6163334/binning-data-in-python-with-scipy-numpy
bin_means_y = (np.histogram(counter_dict.keys(),bins,weights=counter_dict.values())[0] / np.histogram(counter_dict.keys(),bins)[0])
bin_means_x = (np.histogram(counter_dict.keys(),bins,weights=counter_dict.keys())[0] / np.histogram(counter_dict.keys(),bins)[0])
return bin_means_x,bin_means_y
在NetworkX
中生成经典的无标度网络,然后绘制:
import networkx as nx
ba_g = nx.barabasi_albert_graph(10000,2)
ba_c = nx.degree_centrality(ba_g)
# To convert normalized degrees to raw degrees
#ba_c = {k:int(v*(len(ba_g)-1)) for k,v in ba_c.iteritems()}
ba_c2 = dict(Counter(ba_c.values()))
ba_x,ba_y = log_binning(ba_c2,50)
plt.xscale('log')
plt.yscale('log')
plt.scatter(ba_x,ba_y,c='r',marker='s',s=50)
plt.scatter(ba_c2.keys(),ba_c2.values(),c='b',marker='x')
plt.xlim((1e-4,1e-1))
plt.ylim((.9,1e4))
plt.xlabel('Connections (normalized)')
plt.ylabel('Frequency')
plt.show()
生成以下图表,显示蓝色的“原始”分布与红色的“分箱”分布之间的重叠。
如果我错过了一些明显的东西,如何改进这种方法或反馈的想法是受欢迎的。