【问题标题】:pandas matplotlib .plot(kind='hist') vs .plot(kind='bar') issuepandas matplotlib .plot(kind='hist') vs .plot(kind='bar') 问题
【发布时间】:2015-08-18 12:45:49
【问题描述】:

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

【问题讨论】:

  • 你想要什么直方图?你想要一个二阶直方图,即计数的计数吗?或者您只是想将您当前的条形图分组到 bin 中。

标签: python pandas matplotlib histogram bar-chart


【解决方案1】:

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

因此,相当于:

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

应该是:

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

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2020-08-01
  • 2022-12-02
  • 1970-01-01
  • 2017-08-06
  • 1970-01-01
  • 1970-01-01
  • 2021-01-11
  • 2021-07-17
相关资源
最近更新 更多