Create a Matplotlib Pie Chart for Categorical Data in Python

I have spent years building data visualization dashboards for various American tech firms. One thing I have learned is that a well-crafted pie chart is still one of the most effective ways to show categorical proportions.

Whether you are looking at the market share of tech giants or the population distribution across states, Python makes this easy.

In this tutorial, I will show you exactly how to create and customize a Matplotlib pie chart for categorical data using Python.

Python Matplotlib Pie Chart for Categorical Data

Categorical data represents groups or labels rather than continuous numbers. For example, “US States” or “Car Brands” are categories.

A pie chart helps you see how much each category contributes to the whole 100%. I find that using the Matplotlib library in Python provides the most flexibility for these types of visualizations.

Method 1: Create a Basic Python Matplotlib Pie Chart

Let’s start with a simple scenario involving the top 5 most populous US states. I often use this basic structure when I need a quick visual check of my data distribution.

Here is the full Python code to create a basic pie chart:

import matplotlib.pyplot as plt

# Categorical data: Top 5 US States by Population (Estimated)
states = ['California', 'Texas', 'Florida', 'New York', 'Pennsylvania']
population_share = [39, 30, 22, 19, 13]

# Creating the Python Matplotlib pie chart
plt.figure(figsize=(10, 7))
plt.pie(population_share, labels=states)

# Adding a title to our Python visualization
plt.title('Population Distribution of Top 5 US States')

# Display the plot
plt.show()

You can see the output in the screenshot below.

Matplotlib Pie Chart for Categorical Data Python

In this example, I defined a list of states as my categories and their respective population estimates as the values.

The plt.pie() function takes the numerical data first and the labels second.

Method 2: Highlight Data with the Explode Feature

Sometimes, I want to emphasize a specific category, like the largest market leader in the US smartphone industry.

The “explode” feature in Python Matplotlib allows you to pull a slice out from the center.

import matplotlib.pyplot as plt

# Categorical data: US Smartphone Market Share
brands = ['Apple (iPhone)', 'Samsung', 'Google Pixel', 'Motorola', 'Others']
market_share = [55, 30, 5, 5, 5]

# Explode the first slice (Apple) by 0.1 units
explode_values = (0.1, 0, 0, 0, 0) 

# Plotting the Python pie chart with explode
plt.figure(figsize=(10, 7))
plt.pie(market_share, 
        labels=brands, 
        explode=explode_values, 
        shadow=True, 
        startangle=140)

plt.title('US Smartphone Market Share Analysis - Python Tutorial')
plt.show()

You can see the output in the screenshot below.

Matplotlib Pie Chart for Categorical Data in Python

I used the explode parameter here to make the Apple slice stand out. I also added shadow=True to give the chart a professional, 3D-like depth.

Method 3: Add Percentages and Custom Colors in Python

When I present data to stakeholders in the USA, they always ask for exact percentages. Matplotlib handles this calculation automatically using the autopct parameter.

import matplotlib.pyplot as plt

# Categorical data: US Energy Consumption by Source
energy_sources = ['Fossil Fuels', 'Nuclear', 'Renewables', 'Others']
consumption = [60, 18, 20, 2]

# Defining custom colors for our Python plot
custom_colors = ['#ff9999', '#66b3ff', '#99ff99', '#ffcc99']

# Creating the detailed Python Matplotlib pie chart
plt.figure(figsize=(10, 7))
plt.pie(consumption, 
        labels=energy_sources, 
        autopct='%1.1f%%', 
        colors=custom_colors, 
        startangle=90)

plt.title('US Annual Energy Consumption Sources')
plt.axis('equal')  # Equal aspect ratio ensures that pie is drawn as a circle.
plt.show()

You can see the output in the screenshot below.

Matplotlib Pie Chart for Categorical Data

The autopct=’%1.1f%%’ string tells Python to format the values as percentages with one decimal place.

Setting plt.axis(‘equal’) is a trick I always use to ensure the chart remains a perfect circle regardless of the window size.

Method 4: Visualize Python Pandas Categorical Data

In real-world Python development, you will likely work with the Pandas library to handle your datasets.

I frequently use Pandas to read CSV files containing US economic data and then plot them directly.

import matplotlib.pyplot as plt
import pandas as pd

# Creating a dataset of US Tech Company Revenues (In Billions)
data = {
    'Company': ['Apple', 'Microsoft', 'Alphabet', 'Amazon', 'Meta'],
    'Revenue': [383, 211, 307, 574, 134]
}

df = pd.DataFrame(data)

# Plotting directly from the Pandas DataFrame
plt.figure(figsize=(10, 7))
plt.pie(df['Revenue'], labels=df['Company'], autopct='%1.1f%%', startangle=140)

plt.title('Revenue Distribution of Top US Tech Companies')
plt.show()

In this snippet, I converted a Python dictionary into a DataFrame. This approach is highly scalable and is the standard way I handle large-scale categorical data in Python.

Method 5: Add a Legend for Complex Categorical Data

If you have many categories, the labels on the chart can become cluttered. I prefer moving the labels to a separate legend box to keep the visualization clean.

This is especially useful for data like US Car Sales, where there are many competing brands.

import matplotlib.pyplot as plt

# Categorical data: US Car Brand Sales Proportion
car_brands = ['Ford', 'Toyota', 'Chevrolet', 'Honda', 'Nissan', 'Jeep']
sales_data = [20, 18, 15, 12, 10, 25]

plt.figure(figsize=(10, 7))
patches, texts = plt.pie(sales_data, startangle=90)

# Creating a legend for our Python Matplotlib chart
plt.legend(patches, car_brands, loc="best", title="US Car Brands")

plt.title('Market Proportion of Automobile Brands in the USA')
plt.axis('equal')
plt.tight_layout()
plt.show()

By using plt.legend(), I can place the category names anywhere on the canvas. The loc=”best” argument tells Python to find the spot where the legend overlaps the least with the chart.

Best Practices for Python Pie Charts

During my years as a developer, I have found that pie charts are best used when you have fewer than six categories.

If you have too many slices, the chart becomes difficult to read.

In those cases, I recommend switching to a horizontal bar chart.

Always ensure your labels are legible and your colors have enough contrast for accessibility.

Python and Matplotlib provide all the tools you need to make these adjustments easily. I hope this tutorial helped you understand how to master the Matplotlib pie chart for categorical data.

With these methods, you can transform raw US-based datasets into insightful and professional visualizations.

You may also read:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.