Reputation: 678
I am frustrated about percentage ratio in pie chart. I don't why but the percentage was wrong in the pie chart. I think plotting function misunderstood the plotting data then gave me the wrong percentage value in the pie chart. what was the problem? how to fix this? any idea?
my attempt and minimal data
here is minimal data on the public gist.
my attempt:
import pandas as pd
df=pd.read_csv('piechart.csv', encoding='utf-8')
labels = tuple(df.index)
total_sum = df['qty1_sum'].sum()
sizes=[58, 50, 66, 53, 48, 48, 34, 49, 59, 48]
fig1, ax1 =plt.subplots(figsize=(12,8))
pie = ax1.pie(sizes, wedgeprops=dict(width=0.8), autopct= '%1.1f%%',shadow=True, startangle=90, textprops={'fontsize': 12})
tot_sum=str(total_sum) + '\n Metric Tons'
ax1.text(0., 0., tot_sum, horizontalalignment='center', verticalalignment='center')
ax1.axis('equal')
ax1.set(title="top 10 country by export")
ax1.set_axis_off()
ax1.legend(pie[0],top10_cty, loc="upper right", fontsize=20, bbox_to_anchor=(1.25, 1.25))
plt.show()
when I run my code, percent of each country was wrong, for example, Japan should have 29% but in my attempted code, it gave me 11% value. why? how to fix this problem? where is the source of this issue? any quick solution? thanks
why I insist that percentage wrong
I manually calculated the percentage for each country and in pie chart percentage value was wrong. I don't know why. any idea to track down the source of the issue? thanks
Upvotes: 1
Views: 1283
Reputation: 80349
Japan should have 29.97 %, which is rounded to one decimal as 30.0 %.
The principal problem is that the code uses a fixed list sizes=[58, 50, ...]
instead of using the values from the dataframe.
Filling in the correct columns to use for the pie chart and for the labels:
import matplotlib.pyplot as plt
import pandas as pd
df = pd.read_csv('piechart.csv', encoding='utf-8')
total_sum = df['qty1_sum'].sum()
fig1, ax1 = plt.subplots(figsize=(12, 8))
pie = ax1.pie(df['qty1_sum'], wedgeprops=dict(width=0.8), autopct='%1.1f%%', shadow=True, startangle=90,
textprops={'fontsize': 12})
tot_sum = str(round(total_sum)) + '\nMetric Tons'
ax1.text(0., 0., tot_sum, horizontalalignment='center', verticalalignment='center')
ax1.axis('equal')
ax1.set(title="top 10 country by export")
ax1.set_axis_off()
ax1.legend(pie[0], df['cty_ptn'], loc="upper right", fontsize=20, bbox_to_anchor=(1.25, 1.25))
plt.tight_layout()
plt.show()
Upvotes: 2