I have a pie chart in this code:
import matplotlib.pyplot as plt
import pandas as pd
excel_file_path = "Chartdata.xlsx"
df = pd.read_excel(excel_file_path)
df_country = df.groupby(['Country']).sum()
df_country ['Revenue'].plot.pie()
plt.show()
It reads an Excel file, checks if there are more locations for one country and if yes, sums the revenues for the locations in the same country and then displays the data as a pie chart using matplotlib.
This is what I get:
https://i.stack.imgur.com/DWuRp.png
And this is my Excel file:
Location Country Revenue
Loc1 China 23
Loc2 China 48
Loc3 Netherlands 17
Loc4 Germany 21
What I want to do, is to add labels with percentage inside the pie chart and to keep my labels with the countries. I want to read all the data from the Excel file without having to specify the countries inside the code.
How can I do this and is it possible? Can anyone help me please?
>Solution :
One way using autopct:
df_country = df.groupby("Country").sum()
df_country["Revenue"].plot.pie(autopct="%.2f")
Output:
