Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can I add labels with percentage to a pie chart in Python?

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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:

enter image description here

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading