Python: 如何在( value_min, value_max ] 上绘制数据。

问题描述 投票:0回答:2

我有一个巨大的数据集,我想让我们说的bin和绘图。因为当我绘制原始数据时,它看起来像这样... ... 一个非常丑陋的图。enter image description here

基于... 这个 我生成了均值、标准值和范围为1的大小值,并踢掉了NaN值,用下面的代码替换了索引。

test = df.groupby(pd.cut(df['value'], bins=np.arange(160900)))['ratio'].agg(['mean', 'std', 'size'])
test_filtered = test[test[['mean', 'std', 'size']].notnull().all(1)]
test_filtered.reset_index(level=0, inplace=True)

然后我得到这个

               value       mean       std  size
0   (160088, 160089] 17.5080464 0.0777015    43
1   (160089, 160090] 17.5167586 0.0637891    25
2   (160188, 160189] 17.5099577 0.0892071    13
3   (160189, 160190] 17.4971442 0.0917634    60
4   (160288, 160289] 17.5440752 0.0659020    51
5   (160289, 160290] 17.5638237 0.0615202    64
6   (160290, 160291] 17.5382187 0.0294264     2
7   (160388, 160389] 17.5282669 0.1120136     2
8   (160389, 160390] 17.5479696 0.0794665    64
9   (160390, 160391] 17.5716048 0.0892945    15
10  (160391, 160392] 17.4969686 0.0284094     2
11  (160488, 160489] 17.5587446 0.0449601     5
12  (160489, 160490] 17.5566764 0.0636091    62
13  (160490, 160491] 17.5279026 0.0561810     2
14  (160588, 160589] 17.5922320 0.0126914     2
15  (160589, 160590] 17.5832962 0.0733587    25
16  (160590, 160591] 17.5607141 0.0706487    32
17  (160688, 160689] 17.5186035 0.0773348     6
18  (160689, 160690] 17.5234588 0.0816204    51
19  (160690, 160691] 17.4688810 0.0981311     4
20  (160788, 160789] 17.5797546 0.0264994     6
21  (160789, 160790] 17.5517244 0.0470787    51
22  (160790, 160791] 17.5600856 0.0720480     2
23  (160889, 160890] 17.5355430 0.0584237    34

所以现在的问题是,如何在数值上绘制平均值?我试着写了一些代码,但只得到一堆错误......。此外,bin是固定为1的,但也许我需要另一个范围。你知道如何指定比1更多的 "bin窗口 "吗?

或者你知道一个更好的方法,如何用一个特定的 "bin window "来bin数据吗?

先谢谢你,如果你知道如何解决这个问题。 ;)

问候

python dataframe plot mean binning
2个回答
0
投票

如果你 转换您的数据框 到一个numpy数组,然后你可以使用numpy的 直方图 来控制你的bin大小。Numpy数组也可以用以下方法过滤掉NaNs 哪儿.


0
投票
from matplotlib import pyplot as plt
ax = plt.gca()
test_filtered.plot.bar(ax=ax)
plt.xticks(ticks=test_filtered.index, labels=test_filtered.value)
plt.show()

© www.soinside.com 2019 - 2024. All rights reserved.