Search code examples
pythonexcelpie-chart

Creating a pie-chart that is filled up based on values excel?


I want to create a Piechart in excel that fills up based on the values. So, it would have as many slices as rows in the table and based on the row's actual amount, it would fill up the slice accordingly. Below, I have presented a rough sketch of what I'm looking for in the piechart to explain it clearly. Assume there are 8 rows and the totals are out of 100; as no slice is full, there are no rows with 100 in their total. If you think there is a solution that can be coded in python then please could you suggest a place to look?

Text Obviously not to scale


Solution

  • Here's my attempt to your question:

    Sample code:

    # Create sample dataset:
    index= ['A','B','C','D']
    columns= ['pctLoad','pctTotal']
    values= [[35,41],[39,12],[54,22],[27,25]]
    df= pd.DataFrame(values, index=index, columns=columns, dtype= float)
    
    Index pctLoad pctTotal
    A 35 41
    B 39 12
    C 54 22
    D 27 25

    Assume pctLoad as the metric (percentage) by which the variable is used within its own limits, i.e. the bar chart. Where pctTotal represents the percentage share of each variable (A, B, C and D), i.e. the pie chart.

    width= df['pctTotal']/100*2*np.pi
    theta= [0]
    for i, j in enumerate(width):
        theta.append(theta[-1] + j)
    del(theta[-1])
    
    fig, ax= plt.subplots(figsize= (5, 5))
    ax = plt.subplot(projection='polar')
    ax.bar(theta, df['pctLoad'], width= width, align='edge', color= ['tab:blue', 'lightblue','grey', 'lightgrey'], alpha= 0.5, edgecolor= 'k')
    ax.set_xticklabels([])
    plt.show()
    

    Result:

    enter image description here