I’ve worked extensively with data visualization tools. One of the most intuitive and visually appealing ways to present categorical data is through pie charts. When it comes to Python, Matplotlib is my go-to library for creating pie charts that are not only functional but also highly customizable.
In this article, I’ll share my firsthand experience with Matplotlib pie charts. Whether you’re analyzing market share across US companies, visualizing election results, or simply breaking down survey responses, this guide will help you create effective pie charts with ease.
Let’s get into the world of Matplotlib pie charts!
What is a Pie Chart and Why Use Matplotlib?
A pie chart is a circular graph divided into slices to illustrate numerical proportions. It’s a great way to show parts of a whole at a glance.
Matplotlib is a powerful Python plotting library that simplifies the process of creating pie charts. From basic charts to advanced customizations like exploded slices and shadows, Matplotlib offers it all.
Method 1: Create a Basic Pie Chart
Creating a pie chart with Python Matplotlib is easy. Here’s how I usually start:
import matplotlib.pyplot as plt
# Sample data: Market share of top 4 US tech companies (in %)
labels = ['Apple', 'Microsoft', 'Google', 'Amazon']
sizes = [30, 25, 20, 15]
plt.pie(sizes, labels=labels)
plt.title('Market Share of Top US Tech Companies')
plt.show()I executed the above example code and added the screenshot below.

This simple code snippet plots a pie chart with labels. The sizes list holds the proportion of each slice, while labels defines the category names.
Method 2: Add Percentage Labels and Exploding Slices
Sometimes, it’s helpful to show the exact percentage on each slice and highlight a particular segment. I often use the autopct and explode parameters for this.
explode = (0.1, 0, 0, 0) # Explode the first slice (Apple)
plt.pie(sizes, labels=labels, autopct='%1.1f%%', explode=explode, shadow=True, startangle=140)
plt.title('Market Share of Top US Tech Companies')
plt.show()I executed the above example code and added the screenshot below.

autopct='%1.1f%%'adds percentage labels with one decimal place.explodeoffsets the Apple slice to emphasize it.shadow=Trueadds a shadow for a 3D effect.startangle=140rotates the start of the pie chart for better visual balance.
This method makes the chart more informative and visually appealing.
Method 3: Customize Colors and Add a Legend
By default, Matplotlib assigns colors automatically, but customizing colors can improve clarity, especially for presentations or reports.
colors = ['#ff9999','#66b3ff','#99ff99','#ffcc99']
plt.pie(sizes, labels=labels, autopct='%1.1f%%', colors=colors, startangle=90)
plt.title('Market Share of Top US Tech Companies')
plt.legend(labels, loc='upper right')
plt.show()I executed the above example code and added the screenshot below.

Here, I chose soft pastel colors that are easy on the eyes. Adding a legend helps when the chart is complex or when labels overlap.
Method 4: Create a Donut Chart (Pie Chart with a Hole)
Donut charts are a popular variation of pie charts, especially in dashboards. They look modern and help focus on the proportions without the clutter of labels inside the slices.
plt.pie(sizes, labels=labels, autopct='%1.1f%%', colors=colors, startangle=90, wedgeprops={'width':0.4})
plt.title('Market Share of Top US Tech Companies')
plt.show()The key here is the wedgeprops={'width':0.4} which controls the thickness of the slices, creating the donut effect.
Method 5: Handle Multiple Pie Charts for Comparison
Often, you want to compare data side by side. For example, comparing market share changes from 2022 to 2023.
sizes_2022 = [28, 27, 22, 13]
sizes_2023 = [30, 25, 20, 15]
fig, axs = plt.subplots(1, 2, figsize=(10,5))
axs[0].pie(sizes_2022, labels=labels, autopct='%1.1f%%', startangle=90)
axs[0].set_title('Market Share 2022')
axs[1].pie(sizes_2023, labels=labels, autopct='%1.1f%%', startangle=90)
axs[1].set_title('Market Share 2023')
plt.show()Using subplots helps visualize changes clearly and side by side.
Tips from My Experience
- Keep it simple: Avoid too many slices; if you have many categories, consider grouping smaller ones into an ‘Others’ slice.
- Use contrasting colors: This improves readability, especially when printed in black and white.
- Avoid 3D pie charts: They often distort perception and can mislead the viewer.
- Label wisely: Use legends or percentage labels, but not both if the chart becomes cluttered.
Creating pie charts with Matplotlib is a breeze once you understand the basics and some handy customizations. Whether you’re working on business reports, dashboards, or data analysis projects in the US market, these charts make your data speak visually.
Try these methods with your data and tweak the parameters to fit your needs. With Matplotlib, you have full control over the look and feel of your pie charts.
Other Python articles you may also like:

I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.