multiple pie chart for each row pandas

后端 未结 2 2000
名媛妹妹
名媛妹妹 2021-01-23 02:49

I would like to create multiple pie chart for each continent to show the alcohol serving with percentage on it.

Thank you

相关标签:
2条回答
  • 2021-01-23 03:15

    You can use DataFrame.plot.pie with transpose dataframe by T:

    df = pd.DataFrame({'beer':[1,2,3],
                       'spirit':[4,5,6],
                       'wine':[7,8,9]}, index=['Africa','Asia','Europe'])
    
    print (df)
            beer  spirit  wine
    Africa     1       4     7
    Asia       2       5     8
    Europe     3       6     9
    
    df.T.plot.pie(subplots=True, figsize=(10, 3))
    

    0 讨论(0)
  • 2021-01-23 03:19

    here is the code, I found this kind more flexible

    from matplotlib import pyplot as plt
    
    import pandas as pd   
    
    df = pd.DataFrame({'beer':[1,2,3],
                   'spirit':[4,5,6],
                   'wine':[7,8,9]}, index=['Africa','Asia','Europe'])    
    
    df= df.div(df.sum(axis=1), axis=0)
    
    fig, axs = plt.subplots(nrows=df.index.size, ncols=1, figsize=(7,7))
    
    fig.subplots_adjust(hspace=0.5, wspace=0.05)
    
    for row in range(df.index.size + 1):
        fig.add_subplot(axs[row] )
        plt.pie(df.loc[df.index[row],:], labels=df.columns)
        plt.axis('off')
    
    0 讨论(0)
提交回复
热议问题