gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 01:05:37 26 4
gpt4 key购买 nike

我有一个名为 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com