简体   繁体   中英

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

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.

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:

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM