How to Show the actual value instead of the percent in a Matplotlib Pie Chart

Question:

The following code is for creating a pie chart that shows the number of purchases made by each person from the "Shipping Address Name" column. The ‘labels’ list contains the name of each person and the ‘purchases’ list contain the number of purchases each person has made.

labels = df['Shipping Address Name'].unique()

purchases = df['Shipping Address Name'].value_counts()

plt.pie(purchases, labels=labels, autopct='%.f')
plt.title('Purchases Per Person')
plt.axis('equal')
return plt.show()

This is what the ‘purchases’ list basically looks like:

Lilly   269

Rolf    69

Sandy   11

Dan     2

I am trying to have the values in the purchase list to be shown in the pie chart instead of the percentages from "autopct=%.f". Not sure what the easiest way for the ‘autopct’ function to do this is, so any help is appreciated.

Asked By: rod.hoda

||

Answers:

Try re-calculate the actual values by multiplying with the total purchases:

purchases = df['Shipping Address Name'].value_counts()

purchases.plot.pie(autopct=lambda x: '{:.0f}'.format(x*purchases.sum()/100) )
# also
# plt.pie(purchases, autopct=lambda x: '{:.0f}'.format(x*purchases.sum()/100))

Output:

enter image description here

Answered By: Quang Hoang