首页 > 解决方案 > 绘制具有不同字符串组的多个绘图饼图

问题描述

我有一个 T 恤订单列表以及相应的尺寸,我想将它们绘制在饼图中,为每个设计显示尺寸销售最多的百分比等。

    Design      Total
0   Boba L      9
1   Boba M      4
2   Boba S      2
3   Boba XL     5
4   Burger L    6
5   Burger M    2
6   Burger S    3
7   Burger XL   1
8   Donut L     5
9   Donut M     9
10  Donut S     2
11  Donut XL    5

标签: pandasdataframeplotplotlypie-chart

解决方案


目前尚不清楚您的要求,但这是我的解释:

df[['Design', 'Size']] = df['Design'].str.rsplit(n=1, expand=True)

fig, ax = plt.subplots(1, 3, figsize=(10,8))
ax = iter(ax)
for t, g in df.groupby('Design'):
    g.set_index('Size')['Total'].plot.pie(ax=next(ax),  autopct='%.2f', title=f'{t}')

在此处输入图像描述


也许你想要:

df = pd.read_clipboard() #create data from above text no modification
dfplot = df.loc[df.groupby(df['Design'].str.rsplit(n=1).str[0])['Total'].idxmax(), :]

ax = dfplot.set_index('Design')['Total'].plot.pie(autopct='%.2f')
ax.set_ylabel('');

在此处输入图像描述


推荐阅读