2013-05-19 76 views
5

我有以下列表:蟒蛇情节和幂律适合

[6, 4, 0, 0, 0, 0, 0, 1, 3, 1, 0, 3, 3, 0, 0, 0, 0, 1, 1, 0, 0, 0, 3, 2, 3, 3, 2, 5, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 2, 1, 0, 1, 0, 0, 0, 0, 1, 0, 1, 2, 0, 0, 0, 2, 1, 1, 0, 0, 0, 1, 0, 1, 0, 0, 1, 1, 1, 1, 0, 1, 1, 1, 3, 1, 1, 0, 1, 1, 0, 0, 0, 1, 0, 0, 1, 0, 1, 2, 2, 3, 2, 1, 0, 0, 0, 1, 2] 

我想绘制与Python每个实体的频率,并作出分析幂在其上。

但我无法弄清楚如何用ylabel绘制列表的频率和xlabel列表上的数字。

我想创建一个带频率的词典并绘制字典的值,但是用这种方法,我不能把这些数字放在xlabel上。

有什么建议吗?

回答

5

我认为你是正确的词典:

>>> import matplotlib.pyplot as plt 
>>> from collections import Counter 
>>> c = Counter([6, 4, 0, 0, 0, 0, 0, 1, 3, 1, 0, 3, 3, 0, 0, 0, 0, 1, 1, 0, 0, 0, 3, 2, 3, 3, 2, 5, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 2, 1, 0, 1, 0, 0, 0, 0, 1, 0, 1, 2, 0, 0, 0, 2, 1, 1, 0, 0, 0, 1, 0, 1, 0, 0, 1, 1, 1, 1, 0, 1, 1, 1, 3, 1, 1, 0, 1, 1, 0, 0, 0, 1, 0, 0, 1, 0, 1, 2, 2, 3, 2, 1, 0, 0, 0, 1, 2]) 
>>> sorted(c.items()) 
[(0, 50), (1, 30), (2, 9), (3, 8), (4, 1), (5, 1), (6, 1)] 
>>> plt.plot(*zip(*sorted(c.items())) 
...) 
[<matplotlib.lines.Line2D object at 0x36a9990>] 
>>> plt.show() 

这里有几件是感兴趣的。 zip(*sorted(c.items()))将返回类似[(0,1,2,3,4,5,6),(50,30,9,8,1,1,1)]。我们可以使用*运营商解压缩,以便plt.plot可以看到2个参数 - (0,1,2,3,4,5,6)(50,30,9,8,1,1,1)。它们分别用作绘图中的xy值。

至于拟合的数据,scipy可能会在这里有所帮助。具体来说,看看下面的examples。 (其中一个例子甚至使用幂定律)。

+0

我刚才看到您的编辑。谢谢。这可能会解决我的问题。 – Tasos

3
y = np.bincount([6, 4, 0, 0, 0, 0, 0, 1, 3, 1, 0, 3, 3, 0, 0, 0, 0, 1, 1, 0, 0, 0, 3, 2, 3, 3, 2, 5, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 2, 1, 0, 1, 0, 0, 0, 0, 1, 0, 1, 2, 0, 0, 0, 2, 1, 1, 0, 0, 0, 1, 0, 1, 0, 0, 1, 1, 1, 1, 0, 1, 1, 1, 3, 1, 1, 0, 1, 1, 0, 0, 0, 1, 0, 0, 1, 0, 1, 2, 2, 3, 2, 1, 0, 0, 0, 1, 2]) 
x = np.nonzero(y)[0] 
plt.bar(x,y) 

enter image description here

-1
import matplotlib.pyplot as plt 
data = [6, 4, 0, 0, 0, 0, 0, 1, 3, 1, 0, 3, 3, 0, 0, 0, 0, 1, 1, 0, 0, 0, 3, 2, 3, 3, 2, 5, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 2, 1, 0, 1, 0, 0, 0, 0, 1, 0, 1, 2, 0, 0, 0, 2, 1, 1, 0, 0, 0, 1, 0, 1, 0, 0, 1, 1, 1, 1, 0, 1, 1, 1, 3, 1, 1, 0, 1, 1, 0, 0, 0, 1, 0, 0, 1, 0, 1, 2, 2, 3, 2, 1, 0, 0, 0, 1, 2] 

plt.hist(data, bins=range(max(data)+2)) 
plt.show() 

enter image description here

4

使用软件包:幂

import powerlaw 
d=[6, 4, 0, 0, 0, 0, 0, 1, 3, 1, 0, 3, 3, 0, 0, 0, 0, 1, 1, 0, 0, 0, 3,2, 3, 3, 2, 5, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 2, 1, 0, 1, 0, 0, 0, 0, 1,0, 1, 2, 0, 0, 0, 2, 1, 1, 0, 0, 0, 1, 0, 1, 0, 0, 1, 1, 1, 1, 0, 1, 1, 1,3, 1, 1, 0, 1, 1, 0, 0, 0, 1, 0, 0, 1, 0, 1, 2, 2, 3, 2, 1, 0, 0, 0, 1, 2] 
fit = powerlaw.Fit(numpy.array(d)+1,xmin=1,discrete=True) 
fit.power_law.plot_pdf(color= 'b',linestyle='--',label='fit ccdf') 
fit.plot_pdf(color= 'b') 

print('alpha= ',fit.power_law.alpha,' sigma= ',fit.power_law.sigma) 

阿尔法= 1.85885487521 西格玛= 0.0858854875209

enter image description here

它允许正确地绘制,拟合和分析数据。它具有与离散数据拟合幂律分布的特殊方法。

它可以与安装:pip install powerlaw

+0

你是否知道如何得到比例因子C? –