Skip to Content

Pie Charts in Matplotlib

Pie charts are used to display the proportions of parts of a whole. Each slice of the pie represents a category, with its size corresponding to its proportion.


Creating Pie Charts

The pie() function in Matplotlib is used to create pie charts.

Example: Basic Pie Chart

import matplotlib.pyplot as plt # Data labels = ['Python', 'Java', 'C++', 'JavaScript'] sizes = [215, 130, 245, 210] # Create pie chart plt.pie(sizes, labels=labels, autopct='%1.1f%%', startangle=90) # Add title plt.title("Programming Language Popularity") # Display the plot plt.show()

Customizing Pie Charts

Matplotlib provides several parameters to customize pie charts:

ParameterDescriptionExample Value
labelsNames of the categories['A', 'B', 'C']
autopctDisplay format for percentage values'%1.1f%%'
startangleStarting angle of the chart90
colorsColors for each slice['red', 'blue']
explodeOffset for slices (proportional values)[0.1, 0, 0]

Example: Customized Pie Chart

# Data labels = ['Apples', 'Bananas', 'Cherries', 'Dates'] sizes = [20, 30, 25, 25] colors = ['gold', 'yellowgreen', 'lightcoral', 'lightskyblue'] explode = [0.1, 0, 0, 0] # Explode the first slice # Create customized pie chart plt.pie(sizes, labels=labels, autopct='%1.1f%%', startangle=140, colors=colors, explode=explode) # Add title plt.title("Fruit Distribution") # Display the plot plt.show()

Adding Legends and Annotations

Legends and annotations enhance the readability of pie charts.

Example: Pie Chart with Legend

# Data labels = ['Rent', 'Groceries', 'Utilities', 'Entertainment'] sizes = [500, 300, 100, 100] # Create pie chart plt.pie(sizes, labels=labels, autopct='%1.1f%%', startangle=90) plt.title("Monthly Expenses") # Add legend plt.legend(labels, title="Categories", loc="best") # Display the plot plt.show()

Practical Examples

Example 1: Survey Results

# Data labels = ['Satisfied', 'Neutral', 'Dissatisfied'] sizes = [60, 25, 15] colors = ['green', 'yellow', 'red'] # Create pie chart plt.pie(sizes, labels=labels, autopct='%1.1f%%', startangle=90, colors=colors) # Add title plt.title("Customer Satisfaction Survey") # Display the plot plt.show()

Example 2: Market Share

# Data labels = ['Company A', 'Company B', 'Company C', 'Company D'] sizes = [40, 30, 20, 10] # Create pie chart plt.pie(sizes, labels=labels, autopct='%1.1f%%', startangle=90, colors=['blue', 'orange', 'green', 'red']) # Add title plt.title("Market Share Distribution") # Display the plot plt.show()

Try It Yourself

Problem 1: Create a Pie Chart

Create a pie chart to represent the percentage distribution of your daily activities: sleeping, studying, exercising, and leisure.

Show Code

# Data labels = ['Sleeping', 'Studying', 'Exercising', 'Leisure'] sizes = [8, 6, 2, 8] # Create pie chart plt.pie(sizes, labels=labels, autopct='%1.1f%%', startangle=90) # Add title plt.title("Daily Activity Distribution") # Display the plot plt.show()

Problem 2: Favorite Hobbies

Create a pie chart showing the distribution of survey responses for favorite hobbies.

Show Code

# Data labels = ['Reading', 'Traveling', 'Cooking', 'Gaming'] sizes = [25, 30, 20, 25] # Create pie chart plt.pie(sizes, labels=labels, autopct='%1.1f%%', startangle=140, colors=['cyan', 'magenta', 'yellow', 'lime']) # Add title plt.title("Favorite Hobbies") # Display the plot plt.show()

Pie charts are excellent for showing proportions. Experiment with various options to create engaging visualizations that effectively communicate your data.


PygroundTry It Out

Write, run, and experiment with Python code below!

Output:

Last updated on