python - 从 Python 中的嵌套列表制作直方图
问题描述
我有以下列表,我喜欢用这些数据制作直方图,但我不知道该怎么做。
finished = [('https', 38), ('on', 33), ('with', 32), ('model', 28), ('com', 26), ('evaluation', 19), ('detection', 19), ('br', 18), ('models', 18), ("href='g3doc", 17), ('trained', 17)]
我尝试了以下方法:
import matplotlib.pyplot as plt
z=0
for i in finished:
plt.hist(finished[z], bins = range(38))
z=z+1
plt.show()
我总是对标签和值感到困惑。
谢谢你,祝你有美好的一天
解决方案
我会使用这样的条形图:
import matplotlib.pyplot as plt; plt.rcdefaults()
import numpy as np
import matplotlib.pyplot as plt
finished = [('https', 38), ('on', 33), ('with', 32), ('model', 28), ('com', 26), ('evaluation', 19), ('detection', 19), ('br', 18), ('models', 18), ("href='g3doc", 17), ('trained', 17)]
names = list(f[0] for f in finished)
values = list(f[1] for f in finished)
y_pos = np.arange(len(finished))
plt.figure(figsize=(20,10))
plt.bar(y_pos, values, align='center', alpha=0.5)
plt.xticks(y_pos, names)
plt.ylabel('Values')
plt.title('Word usage')
plt.show()
使用不同的数据格式可能会更好。但这适用于您的示例数据。
推荐阅读
- mql4 - FileOpen 在 MQL4 中有时会失败
- python - 如何创建随机订单列表并包含相同的数字?
- c++ - 在 QT VS Tools 中打开 QT 项目文件并正确设置平台工具集和语言
- assembly - 为什么 X86 阶乘函数会收到段错误?
- typescript - 在 Typescript 中为一种语言制作智能感知和语法
- c++ - 部署 C++ 可执行文件,没有库 dll 文件
- python - 从单词列表中删除引号和双引号
- javascript - newarray 没有被正确推送?
- node.js - 如何使用 puppeteer 从代码“frame.evaluate”中监听事件?
- javascript - Firebase 自定义声明实际上并没有为我设置 - Python 后端