Tas*_*sos 7 python matplotlib power-law
我有以下列表:
[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]
Run Code Online (Sandbox Code Playgroud)
我想用python绘制每个实体的频率,并对其进行幂律分析.
但我无法想象如何用ylabel绘制列表频率和xlabel列表中的数字.
我想用频率创建一个字典并绘制字典的值,但是这样,我不能把数字放在xlabel上.
有什么建议?
使用包:powerlaw
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)
Run Code Online (Sandbox Code Playgroud)
alpha = 1.85885487521 sigma = 0.0858854875209
它允许正确地绘制,拟合和分析数据.它具有适合具有离散数据的幂律分布的特殊方法.
它可以安装: pip install powerlaw
我认为你对这本字典说得对:
>>> 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()
Run Code Online (Sandbox Code Playgroud)
这里有一些有趣的部分.  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).它们分别用作绘图中的值x和y值.
至于拟合数据,scipy可能会有一些帮助.具体来说,请看下面的例子.(其中一个例子甚至使用幂律).
|   归档时间:  |  
           
  |  
        
|   查看次数:  |  
           6123 次  |  
        
|   最近记录:  |