python 生成关键字的直方图/帕累托图

标签 python numpy matplotlib highcharts

我有一个巨大的单词列表,例如['abc', 'def', 'python', 'abc', 'python', ...]

  1. 如何生成可以绘制成直方图/帕累托图的列表/字典,例如: {'python': 10, 'abc': 8, 'def': 2,...}

  2. 此外,什么是合适的图表库来可视化上述单词出现率从高到低排序?

最佳答案

collections.Counter提供了一种方便且相对快速的方法来创建像您展示的那样的字典:

from collections import Counter

x = ['spam', 'ham', 'eggs', 'ham', 'chips', 'eggs',  'spam', 'spam', 'spam']

counts = Counter(x)
print(counts)
# Counter({'spam': 4, 'eggs': 2, 'ham': 2, 'chips': 1})

要可视化计数,您可以使用 matplotlib条形图:

from matplotlib import pyplot as plt
import numpy as np

# sort counts in descending order
labels, heights = zip(*sorted(((k, v) for k, v in counts.items()), reverse=True))

# lefthand edge of each bar
left = np.arange(len(heights))

fig, ax = plt.subplots(1, 1)
ax.bar(left, heights, 1)
ax.set_xticks(left + 0.5)
ax.set_xticklabels(labels,  fontsize='large')

enter image description here

关于python 生成关键字的直方图/帕累托图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33370669/

相关文章:

python - 如何在我的特定条件下在数据框中添加列?

python - 如何分离等于阈值的灰度图像区域?

python - 基于第三个变量的带有颜色条的 2 个变量的散点图

python - matplotlib 自定义图例中类别的副标题

python - Pandas 在执行 groupby 后重置索引并保留选择性列

python - pyplot 不导入,抛出错误

python - Selenium - Python 在查找元素时设置超时

python - 计算矩阵中小于一个值的所有值

python - 对 numpy 数组进行排序时输出错误

python - matplotlib - 更改图大小但保持字体大小不变