Search code examples
pythonpython-3.xpython-2.7matplotlibpie-chart

Create a pie with matplotlib


Hello I have this code with Python :

import matplotlib.pyplot as plt

total_a = 0.004095232
total_b = 0.05075945
total_c = 0.005425
total_d = 0.022948572
total_e = 0.015012

slices = [total_a,total_b,total_c,total_d,total_e]
activities = ['a', 'b', 'c', 'd','e']
cols = ['gold', 'yellowgreen', 'lightcoral', 'lightskyblue', 'orangered']
plt.pie(slices,labels=activities,autopct='%1.1f%%',colors=cols,startangle=140,shadow=True)
plt.show()

But I get this when I execute this code :

Pie

I don't understand why I don't get a full pie ? Thank you for your help !


Solution

  • As per ImportanceOfBeingErnest's answer below with an explanation of the documentation, plt.py takes an array input and will normalise the values for you as long as the sum of the elements is greater than 1.

    Since your values sum to less than 1 the array is not normalised. In order to normalise yourself you should should divide each element by the largest value of the list with the following line:

    slices = [aSlice/max(slices) for aSlice in slices]
    

    And I would put this in your program here:

    import matplotlib.pyplot as plt
    
    total_a = 0.004095232
    total_b = 0.05075945
    total_c = 0.005425
    total_d = 0.022948572
    total_e = 0.015012
    
    
    slices = [total_a,total_b,total_c,total_d,total_e]
    slices = [aSlice/max(slices) for aSlice in slices]
    
    activities = ['a', 'b', 'c', 'd','e']
    cols = ['gold', 'yellowgreen', 'lightcoral', 'lightskyblue', 'orangered']
    plt.pie(slices,labels=activities,autopct='%1.1f%%',colors=cols,startangle=140,shadow=True)
    plt.show()
    

    For me this then produces a graph like this:

    The properly rendered pie chart