8

I want my piechart that I have created using matplotlib to show the actual value rather than just the percentge. Here is my code:

pie_shares= [i for i in mean.values()]
positions = [i for i in mean.keys()]
plt.pie(pie_shares,labels=positions, autopct='%1.1f%%', )
plt.show()
jd55
  • 133
  • 1
  • 3
  • 7

1 Answers1

9

If you want to display the actual value of the slices of your pie chart you must provide to the labels those values:

def autopct_format(values):
    def my_format(pct):
        total = sum(values)
        val = int(round(pct*total/100.0))
        return '{v:d}'.format(v=val)
    return my_format
plt.pie(pie_shares, labels = positions, autopct = autopct_format(pie_shares))

In the matplotlib resources, it is mentioned that autopct can be a string format and a function, so, we create a custom function that formats each pct to display the actual value from the percentages used commonly by this feature.