making a stacked barchart in pandas

前端 未结 1 1377
渐次进展
渐次进展 2020-12-18 04:52

I would like to create a stacked bar plot from the following dataframe:

   VALUE     COUNT  RECL_LCC  RECL_PI
0      1  15686114         3        1
1      2          


        
相关标签:
1条回答
  • 2020-12-18 05:39

    I've put data shown in stackpandas.dat. Given those data:

    from pandas import *
    import matplotlib.pyplot as plt
    
    df = read_table("stackpandas.dat"," +",engine='python')
    
    df = df.convert_objects(convert_numeric=True)
    sub_df1 = df.groupby(['RECL_LCC'])['COUNT'].sum()
    sub_df2 = df.groupby(['RECL_PI'])['COUNT'].sum()
    sub_df = concat([sub_df1,sub_df2],keys=["RECL_LCC","RECL_PI"]).unstack()
    sub_df.plot(kind='bar',stacked=True,rot=1)
    plt.show()
    

    ... gives: enter image description here

    ... which I think is what is sought.

    0 讨论(0)
提交回复
热议问题