python - pandas matplotlib .plot(kind ='hist') vs .plot(kind ='bar') 问题

标签 python pandas matplotlib histogram bar-chart

我有一个名为 firstperiod 的 pandas 数据框和一个名为 megaball 的列。 megaball 的取值范围是 1 到 25,这行代码:

print firstperiod.megaball.value_counts().sort_index()

给我这个,这是我想看到的(每个可能值的出现次数)

1     12
2      4
3      9
4      4
5      3
6      6
7      5
8      8
9      7
10    10
11     6
12     5
13     3
14     5
15     6
16     8
17    15
18     7
19     8
20     5
21     8
22     7
23     1
24    11
25     9


firstperiod.megaball.value_counts().sort_index().plot(kind='bar')
plt.show()

^这向我展示了一个很好的条形图,x 轴值最大为 25,y 轴值最大为 15。

但出于某种原因,当我想要直方图而不是条形图(并且只更改 kind= 的参数值时,这给了我一些完全不正确的东西,并且与条形图值非常不同较早。这是为什么?如何修复直方图?

firstperiod.megaball.value_counts().sort_index().plot(kind='hist')
plt.show()

最佳答案

那是因为“历史”图不仅仅是绘制数据,而是实际上是先估计原始数据的经验分布,然后绘制结果。也就是说,“hist”将对数据进行分箱,计算每个分箱的实例并绘制它,因此我们不需要执行 value_counts()

因此,相当于:

firstperiod.megaball.value_counts().sort_index().plot(kind='bar')

应该只是:

firstperiod.megaball.plot(kind='hist')

关于python - pandas matplotlib .plot(kind ='hist') vs .plot(kind ='bar') 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30634812/

相关文章:

python - 读取 Windows 文件而不阻止其他进程写入该文件

python - 如何处理 lxml 中的编码以正确解析 html 字符串?

python - 如何从 api 响应中提取 application/zip?

python - 在 matplotlib 中迭代添加总数未知的子图

python - 使用 Get2D 在 OpenCV 中访问二维像素值时出现超出范围错误或返回值错误

python - 使用 python 将视频 session 中的音频流传输到 azure 语音翻译

python - 合并两个 DataFrame

python - 如何将数据帧乘以行

python - pycharm 中的 Jupyter notebook 不显示内联图

python - 为什么绘图函数 plt.show() 在循环内部或外部时表现不同?