python怎么根据度分布生成网络,绘制日志绑定的网络度分布

I have often encountered and made long-tailed degree distributions/histograms from complex networks like the figures below. They make the heavy end of these tails, well, very heavy and crowded from many observations:

R7RzP.png

However, many publications I read have much cleaner degree distributions that don't have this clumpiness at the end of the distribution and the observations are more evenly-spaced.

!

5cc3d8f6af1b728279af24afff1cfb5d.png

How do you make a chart like this using NetworkX and matplotlib?

解决方案

Use log binning (see also). Here is code to take a Counter object representing a histogram of degree values and log-bin the distribution to produce a sparser and smoother distribution.

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

Generating a classic scale-free network in NetworkX and then plotting this:

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()

Produces the following plot showing the overlap between the "raw" distribution in blue and the "binned" distribution in red.

f07d379cb5b138dfa34c94ca983566ff.png

Thoughts on how to improve this approach or feedback if I've missed something obvious are welcome.

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值