2013-05-19 72 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? –