简体   繁体   English

使用matplotlib绘制百分位数

[英]Plot percentiles using matplotlib

I have three dataframes df1, df2 and df3. 我有三个数据框df1,df2和df3。 I combine these into one dataframe df. 我将它们合并到一个数据帧df中。 Now i want to find the min, 5 percentile, 25 percentile, median, 90 percentile and max for each date in the dataframe and plot it (line graph for each date) where X axis has the percentiles and Y axis has the values. 现在,我想在数据框中找到每个日期的最小值,5%,25%,中位数,90%和最大值,并将其绘制(每个日期的线图),其中X轴具有百分位数,Y轴具有值。

df1
    date          value
0   2017-11-06    10.20
1   2017-11-06    40.20
2   2017-11-06    35.10
3   2017-11-06    90.45
4   2017-11-06    60.23

df2
    date          value
1   2017-11-07    110.20
2   2017-11-07    500.26
3   2017-11-07    200.16
4   2017-11-07    350.01
5   2017-11-07    89.20

df3
    date          value
1   2017-11-08    101.45 
2   2017-11-08    160.34
3   2017-11-08    41.54
4   2017-11-08    192.42
5   2017-11-08    111.12


df

    date          value
0   2017-11-06    10.20
1   2017-11-06    40.20
2   2017-11-06    35.10
3   2017-11-06    90.45
4   2017-11-06    60.23
5   2017-11-07    110.20
6   2017-11-07    500.26
7   2017-11-07    200.16
8   2017-11-07    350.01
9   2017-11-07    89.20
10  2017-11-08    101.45 
11  2017-11-08    160.34
12  2017-11-08    41.54
13  2017-11-08    192.42
14  2017-11-08    111.12

IIUC, use groupby + agg / quantile - IIUC,使用groupby + agg / quantile -

g = df.groupby('date')

i = g['value'].quantile([0.05, 0.25, 0.5, 0.9]).unstack()
j = g['value'].agg(['min', 'max'])

pd.concat([i, j], 1)

              0.05    0.25     0.5      0.9    min     max
date                                                      
2017-11-06  15.180   35.10   40.20   78.362  10.20   90.45
2017-11-07  93.400  110.20  200.16  440.160  89.20  500.26
2017-11-08  53.522  101.45  111.12  179.588  41.54  192.42

For the plot, this should suffice - 对于剧情,这应该足够了-

i.T.plot(subplots=True)
plt.show()

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM