Plot stacked bar chart from pandas data frame

前端 未结 2 602
礼貌的吻别
礼貌的吻别 2021-01-16 06:30

I have dataframe:

payout_df.head(10)

What would be the easiest, smartest and fastest way to replicate the following excel plot?

<
2条回答
  •  自闭症患者
    2021-01-16 07:04

    Besides the lack of data, I think the following code will produce the desired graph

    import pandas as pd
    import matplotlib.pyplot as plt
    
    df.payout = pd.to_datetime(df.payout)
    
    grouped = df.groupby(pd.Grouper(key='payout', freq='M')).sum()
    grouped.plot(x=grouped.index.year, kind='bar', stacked=True)
    
    plt.show()
    

    I don't know how to reproduce this fancy x-axis style. Also, your payout column must be a datetime, otherwise pd.Grouper won't work (available frequencies).

提交回复
热议问题