【问题标题】:multiple pie chart for each row pandas每行熊猫的多个饼图
【发布时间】:2017-10-29 09:52:21
【问题描述】:

我想为每个大洲创建多个饼图,以显示酒精含量和百分比。

谢谢

【问题讨论】:

    标签: pandas matplotlib dataframe charts


    【解决方案1】:

    您可以将DataFrame.plot.pieT 转置数据帧一起使用:

    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))
    

    【讨论】:

      【解决方案2】:

      这是代码,我发现这种更灵活

      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')
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-01-22
        • 1970-01-01
        • 2014-09-23
        • 2019-08-03
        • 1970-01-01
        • 2023-02-04
        • 2021-07-07
        • 2020-08-31
        相关资源
        最近更新 更多