Percentage labels help show parts of a whole clearly in charts. They make it easy to see how big each piece is compared to the total.
0
0
Percentage labels in Matplotlib
Introduction
Showing how much each category contributes to total sales in a pie chart.
Displaying the percentage of votes each candidate got in an election.
Visualizing the share of different expenses in a monthly budget.
Comparing proportions of different product types sold in a store.
Syntax
Matplotlib
plt.pie(data, labels=labels, autopct='%1.1f%%')autopct adds percentage labels on the pie slices.
The format string '%1.1f%%' shows one decimal place and a percent sign.
Examples
Shows percentages with no decimal places.
Matplotlib
plt.pie([30, 70], labels=['A', 'B'], autopct='%1.0f%%')
Shows percentages with two decimal places.
Matplotlib
plt.pie([10, 20, 30], labels=['X', 'Y', 'Z'], autopct='%1.2f%%')
Uses a function to format percentage labels.
Matplotlib
plt.pie([5, 15], labels=['Cat1', 'Cat2'], autopct=lambda p: f'{p:.1f}%')
Sample Program
This code creates a pie chart showing the percentage of each fruit type. The autopct adds percentage labels with one decimal place.
Matplotlib
import matplotlib.pyplot as plt sizes = [40, 35, 25] labels = ['Apples', 'Bananas', 'Cherries'] plt.pie(sizes, labels=labels, autopct='%1.1f%%') plt.title('Fruit Distribution') plt.show()
OutputSuccess
Important Notes
Percentage labels help viewers understand data proportions quickly.
You can customize the format string to show more or fewer decimals.
Using a function for autopct allows full control over label text.
Summary
Percentage labels show how big each slice is compared to the whole.
Use autopct in plt.pie() to add them easily.
Format strings or functions control how percentages appear.