【问题标题】:Why are my histogram bars all displaying frequencies of 1为什么我的直方图条都显示频率为 1
【发布时间】:2018-04-23 04:11:17
【问题描述】:

我有一个带有索引时间戳和百分比(astype float)的系列(114 行)。

testseries.head()
Out[100]: 
Timestamps
2018-04-19 13:23:57-04:00    0.000161238
2018-04-06 13:59:50-04:00     -0.0169348
2018-04-04 11:39:41-04:00      0.0475188
2018-04-03 14:53:37-04:00    -0.00231244
2018-03-29 14:09:57-04:00      0.0209815
Name: Change, dtype: object

我正在尝试创建这些分布的直方图,就像我之前做过几次一样,但是当我调用时得到了意想不到的结果

testseries.hist()

link to image of output hist

我尝试了各种选项,例如设置 density=True、更改 bin 的数量或在 matplotlib 与 pandas 中绘图,但结果始终是一系列高度等于 y- 最大值的细条轴。

这是什么原因造成的?

【问题讨论】:

    标签: python python-3.x pandas matplotlib


    【解决方案1】:

    直方图正确显示每个值出现一次。为了显示更平滑的内容,您可能希望按分位数和计数对计数进行分组,显示结果的直方图:

    testseries.groupby(pd.cut(testseries.astype(float), 10)).sum().hist()
    

    示例

    import pandas as pd
    import numpy as np
    
    testseries = pd.Series(np.random.randn(100000))
    
    testseries.groupby(pd.cut(testseries.astype(float), 10)).sum().hist();
    

    【讨论】:

    • 谢谢,很明显!在浮动之前,我在 df 列上使用过 .hist() ,并将它们整齐地分组。知道为什么它有时会起作用吗?
    • 它只取决于浮点数的分布。每个人都有可能只出现一次,但不一定是这样。
    猜你喜欢
    • 1970-01-01
    • 2015-10-11
    • 2014-04-22
    • 2011-08-14
    • 1970-01-01
    • 1970-01-01
    • 2020-07-25
    • 2020-01-27
    • 2017-08-10
    相关资源
    最近更新 更多