Search code examples
pandasdataframeplotplotlypie-chart

Plotting multiple plotly pie chart with different string group


I have a list of t-shirt orders along with the corresponding size and I would like to plot them in pie chart for each design showing the percentage in which size sells the most etc.

    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

Solution

  • It is not complete clear what you asking, but here is my interpretation:

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

    enter image description here


    Maybe you want:

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

    enter image description here