简体   繁体   English

从groupby数据框创建条形图

[英]Creating bar chart from a groupby dataframe

  • I have transposed data to the following groupby below and it produces the output at the bottom 我已经将数据转置到下面的groupby,并且在底部产生了输出
  • I don't know how to pull sub-level data to create two tables. 我不知道如何提取子级数据来创建两个表。 One for The PUTs and the other for CALLs. 一个用于PUT,另一个用于CALL。
    • Where the x-axis = strike X轴=触击
    • y-axis = oi sum y轴= oi总和
    • I want to use matplotlib to overlay the two bar charts into one. 我想使用matplotlib将两个条形图叠加为一个。 Like attached image 喜欢附上图片

Any pointers or better way to create this chart from output below? 从下面的输出中创建此图表的任何指针或更好的方法? 在此处输入图片说明 .

df_OI = df[['oi','strike','type','expiry']].groupby([pd.Grouper(freq='D'),'strike','type','expiry']).agg(['sum'])

Created                                     oi
                                    sum
datetime   strike type expiry          
2017-09-29 130.0  PUT  2017-10-06    12
           145.0  PUT  2017-10-06    21
           147.0  PUT  2017-10-06     1
           149.0  PUT  2017-10-06     2
           150.0  CALL 2017-10-06     1
                  PUT  2017-10-06   111
           152.5  CALL 2017-10-06    12
                  PUT  2017-10-06   100
           155.0  CALL 2017-10-06    10
                  PUT  2017-10-06    66
           157.5  CALL 2017-10-06     8
                  PUT  2017-10-06    29
           160.0  CALL 2017-10-06    21
                  PUT  2017-10-06  1405
           162.5  CALL 2017-10-06    14
                  PUT  2017-10-06   327
           165.0  CALL 2017-10-06    56
                  PUT  2017-10-06  2383
           167.5  CALL 2017-10-06    12
                  PUT  2017-10-06  2804
           170.0  CALL 2017-10-06   354
                  PUT  2017-10-06  1945
           172.5  CALL 2017-10-06   438
                  PUT  2017-10-06  3022
           175.0  CALL 2017-10-06  3373
                  PUT  2017-10-06  2093
           177.5  CALL 2017-10-06  1688
                  PUT  2017-10-06  1201
           180.0  CALL 2017-10-06  8241
                  PUT  2017-10-06  2269
           182.5  CALL 2017-10-06  2152
                  PUT  2017-10-06   219
           185.0  CALL 2017-10-06  4008
                  PUT  2017-10-06   469
           187.5  CALL 2017-10-06  2006
                  PUT  2017-10-06   270
           190.0  CALL 2017-10-06  1354
                  PUT  2017-10-06    37
           192.5  CALL 2017-10-06   484
           195.0  CALL 2017-10-06   377
           197.5  CALL 2017-10-06    31
           200.0  CALL 2017-10-06   109
                  PUT  2017-10-06     3
           202.5  CALL 2017-10-06     5
           205.0  CALL 2017-10-06     5

Because I have 2 columns that I'm charting with bar , my total space is 2 . 因为我有2个要用bar绘制的列,所以我的总空间为2 So I use the width=2 to make both bars span a width of 2. 因此,我使用width=2来使两个条形图的宽度均为2。

df.reset_index().set_index(['strike', 'type'])['sum'].unstack() \
  .plot.bar(width=2, alpha=.5, figsize=(10, 4), ec='white')

在此处输入图片说明

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

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