首页 > 解决方案 > How to calculate proportion display pie chart in pandas or others?

问题描述

According to this question, I would like to get the proportion of item in total amount, and display a pie chart like below(need to display the name and the proportion of the item):

screenshot

I use the same dataset as the question:

id      date    num     name    desc    price
1   7/6/2020    10      pen     abcd     $1
1   7/6/2020    2       abc     efg      $3
1   7/6/2020    3       bcd     efg      $5
2   7/6/2020    3       pen     abcd     $1
2   7/6/2020    1       pencil  abcd     $3
2   7/6/2020    2       disk     abcd    $1
2   7/6/2020    2       paper    abcd    $1
3   7/6/2020    2       ff       pag     $100
3   7/6/2020    10      water    kml     $5
4   7/15/2020   5       gg       kml     $5
4   7/15/2020   10      cofffee  oo      $5
5   7/15/2020   5       pp      oo       $4
6   7/15/2020   2       abc    efg        $3
6   7/15/2020   3       bcd    efg        $5
6   7/15/2020   4       aa      efg        $5
6   7/15/2020   5       bb       efg        $6
7   7/15/2020   1       bag       abcd      $50
7   7/15/2020   1       box      abcd       $20
8   7/15/2020   1       pencil    abcd      $3
8   7/15/2020   2       disk     abcd      $1
8   7/15/2020   2       paper    abcd      $1
8   7/15/2020   2       ff       hijk     $100
9   8/15/2020   10      water    kml     $5
9   8/15/2020   5       gg        kml     $5
9   8/15/2020   10      cofffee   oo     $5
9   8/15/2020   5       pp       oo       $4
9   8/15/2020   2       abc      efg        $3
10  8/15/2020   3       bcd      efg        $5
10  8/15/2020   4       aa        efg        $5
10  8/15/2020   5       bb        efg        $6
11  8/15/2020   1       bag       abcd      $50
11  8/15/2020   1       box       abcd      $20

I would like to use matplotlib, pycharts and any other packages are fine, My code as below is not correct:

import pandas as pd
import xlrd
import seaborn as sns
import matplotlib.pyplot as plt

df = pd.read_excel ('./orders.xlsx', sheet_name='Sheet1')
df.groupby(by=['name']).sum()


df['price'] = df['price'].replace('$','', regex=True).astype(int)
df['new'] = df['price'].mul(df['num'])

df1 = df.groupby(by=['name'], as_index=False)['new'].sum()

# df1
# df1['new'] = df1.apply(lambda x: x.sum(), axis=1)
# df1.loc['new'] = df1.apply(lambda x: x.sum()).dropna()
# df1
# data = df1.Series(???)
# # print(data)

# from matplotlib.font_manager import FontProperties   
# # myfont=FontProperties(fname=r'C:/Windows/Fonts/simhei.ttf',size=14)
# # sns.set(font=myfont.get_name())

# plt.rcParams['figure.figsize'] = (8.0, 6.0)   

# lbs= data.index
# explodes=[0.1 if i=='??' else 0 for i in lbs]
# plt.pie(data, explode=explodes,labels=lbs, autopct="%1.1f%%",
#                                 colors=sns.color_palette("muted"),startangle = 90,pctdistance = 0.6,
#           textprops={'fontsize':14,'color':'black'})


# plt.axis('equal')  

# plt.show()

标签: pythonpandasmatplotlib

解决方案


您可以使用 pandas 内部绘图功能,它很可能使用 matplotlib 后端:

s = df.groupby('name')['new'].sum()

ax = s.plot.pie(figsize=(10,10), autopct='%.2f%%')

或链接在一起:

ax = (df.groupby('name')['new'].sum()
        .plot.pie(figsize=(10,10), autopct='%.2f%%')
     )

然后ax是一个 Matplotlib 轴实例,你得到:

在此处输入图像描述


推荐阅读