当数据在元组中时如何在matplotlib中绘制直方图?

Jac*_*yer 1 python matplotlib

我需要绘制列表中最常出现的 5 个单词的直方图。我使用 collections 模块的 c.counter().most_common() 给我以下元组:

[('you', 7706), ('i', 6570), ('we', 2733), ('my', 2718), ('he', 2369)]
Run Code Online (Sandbox Code Playgroud)

当数据采用格式(“词”,频率)时,如何绘制直方图?

我熟悉的格式是: ['you', 'you', 'you', ... , 'i', 'i', 'i', ... , etc.]

我知道我可以将字符串乘以每个元素中的整数,以我熟悉的格式构建一个新列表以在直方图上绘制,但我觉得必须有一种更有效的方法来做到这一点。

Mr.*_*. T 5

解压缩元组列表:

from matplotlib import pyplot as plt

a = [('you', 7706), ('i', 6570), ('we', 2733), ('my', 2718), ('he', 2369)]

plt.bar(*zip(*a))
plt.show()
Run Code Online (Sandbox Code Playgroud)

示例输出: 在此处输入图片说明