plt.figure()
plt.bar([key for val,key in lst], [val for val,key in lst])
plt.xlabel("Terms")
plt.ylabel("Counts")
plt.show()
我有一个元组(count, term)
的列表,该列表已按count
的降序排序(即,该术语在本文档中出现的次数),并如上所述绘制数据。现在假设我想证明在计算语言学中项的分布违反了齐普夫定律,是否可以在不改变x轴的情况下向该图中添加齐普夫曲线(f = c / rank)?怎么样?
[内部,分类x轴编号为0,1,2,...
。要在相同位置绘制曲线,请将range(len(lst))
用于x值。
import matplotlib.pyplot as plt
from scipy.stats import zipf
lst = [(60462, 'Italy'), (46755, 'Spain'), (10423, 'Greece'), (10197, 'Portugal'), (8737, 'Serbia'), (4105, 'Croatia'),
(3281, 'Bosnia and\nHerzegovina'), (2878, 'Albania'), (2083, 'North\nMacedonia'), (2079, 'Slovenia'),
(628, 'Montenegro'), (442, 'Malta'), (77, 'Andorra'), (34, 'San Marino'), (34, 'Gibraltar'), (1, 'Holy See')]
plt.bar([key for val, key in lst], [val for val, key in lst], color='limegreen')
alpha = 1.37065874
total = sum([p for p, c in lst])
plt.plot(range(len(lst)), [zipf.pmf(p, alpha) * total for p in range(1, len(lst) + 1)], color='crimson', lw=3)
plt.ylabel("Population")
plt.xticks(rotation='vertical')
plt.tight_layout()
plt.show()