【发布时间】:2021-09-01 15:15:54
【问题描述】:
这是数据,如何使用 matplotlib 在此离散数据上绘制直方图。
X F
3 3
4 3
5 5
6 3
7 4
8 4
9 3
10 4
11 2
12 2
13 3
14 1
15 2
16 5
17 2
18 1
19 2
20 1
【问题讨论】:
-
df.set_index('X').plot.bar()? -
这可能不是最佳的,但这里是最简单的解决方案:将值存储为:
values = [x[i] for i in range(len(x)) for j in range(f[i])],并使用 hist() 模块与此值数组。x中的每个值x[i]重复f[i]次。 -
因为这已经在数据框中:
df.plot(kind='bar', x='X', ec='k', width=1)
标签: python pandas matplotlib statistics histogram