0
0
Matplotlibdata~5 mins

Basic pie chart with plt.pie in Matplotlib

Choose your learning style9 modes available
Introduction

A pie chart helps you see parts of a whole in a simple circle. It shows how big each part is compared to the total.

You want to show how a budget is split into categories like food, rent, and fun.
You want to display the percentage of votes each candidate got in an election.
You want to compare how much time you spend on different activities in a day.
You want to visualize the market share of different brands in a simple way.
Syntax
Matplotlib
plt.pie(sizes, labels=labels, autopct='%1.1f%%')

sizes is a list of numbers showing the size of each slice.

labels is a list of names for each slice.

autopct shows the percentage on each slice.

Examples
Basic pie chart with three slices and labels.
Matplotlib
sizes = [30, 20, 50]
labels = ['Apples', 'Bananas', 'Cherries']
plt.pie(sizes, labels=labels)
Pie chart showing percentages on each slice.
Matplotlib
sizes = [10, 40, 50]
labels = ['Cats', 'Dogs', 'Birds']
plt.pie(sizes, labels=labels, autopct='%1.1f%%')
Pie chart with equal parts and no decimal in percentages.
Matplotlib
sizes = [25, 25, 25, 25]
labels = ['Q1', 'Q2', 'Q3', 'Q4']
plt.pie(sizes, labels=labels, autopct='%1.0f%%')
Sample Program

This code creates a pie chart showing the popularity of different pets. Each slice shows the percentage and label.

Matplotlib
import matplotlib.pyplot as plt

sizes = [15, 30, 45, 10]
labels = ['Cats', 'Dogs', 'Rabbits', 'Hamsters']

plt.pie(sizes, labels=labels, autopct='%1.1f%%')
plt.title('Pet Popularity')
plt.show()
OutputSuccess
Important Notes

Make sure the sizes add up to 100 or represent parts of a whole.

You can add startangle=90 to rotate the chart for better look.

Use plt.axis('equal') to make the pie circle perfectly round.

Summary

A pie chart shows parts of a whole in a circle.

Use plt.pie() with sizes and labels to create it.

Adding autopct shows percentages on slices.