Skip to main content
 首页 » 编程设计

python之为什么我的直方图条都显示频率为 1

2025年01月19日10cyq1162

我有一个带有索引时间戳和百分比(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- 最大值的细条轴。

这是什么原因造成的?

请您参考如下方法:

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

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();