Plot Pie Charts in Python with Matplotlib
Ever wondered how to make pie charts in Python that don't look like your math textbook from 6th grade? We're serving some sweet plots with Matplotlib today. Whether you're a data newbie or just here to make things look good, this guide's got you covered.
Why Use Pie Charts?
Because sometimes, you just want to show how the cake is divided. Pie charts make it super easy to visualize proportions — who got the biggest slice, who got none, and everything in between. They're quick, clean, and instantly understandable, especially when dealing with simple categorical data.
What You'll Learn
- How to plot pie charts with just a few lines of Python
- Customize slices with colors, labels, and explode effects
- When to use pie charts (and when not to)
- Pro tips to make your visuals pop for presentations & projects
- Master
plt.pie()with multiple data slices — one chart, many stories
New to Python? Installing Matplotlib is easy
Just open your terminal and run: pip install matplotlib. That's all you need to get started!
Step-by-Step Code Example
Here's a simple example of how to create a pie chart in Matplotlib:
import matplotlib.pyplot as plt
# Meme names
memes = [
"Pepe the Frog",
"Doge",
"This Is Fine Dog",
"Distracted Boyfriend",
"Woman Yelling at a Cat",
"Grumpy Cat"
]
# Years they've been famous (as of 2025)
years_famous = [17, 12, 12, 8, 6, 13]
colors = ['#9fcbf5', '#67a6e0', '#2b85d9', '#1862a8', '#1b4f80', '#073b6b']
plt.pie(years_famous, labels=memes, autopct='%1.1f%%', startangle=140, colors=colors)
plt.title("Top 6 Memes by Years of Popularity (as of 2025)",
fontdict={'fontsize': 16, 'fontname': 'Comic Sans MS'})
plt.show()
This pie chart visualizes how long the top 6 legendary internet memes have been famous as of 2025.
Quick Highlights
memes = [...]— 6 legendary memes, each one a certified iconyears_famous = [...]— how many years each meme has ruled the internetcolors = [...]— custom shades to keep things clean and not chaoticplt.pie(...)— draws the pie with labels, % values, colors, and a starting angleautopct='%1.1f%%'— shows percentage with 1 decimal placestartangle=140— rotates the pie for better visual balanceplt.title(...)— adds a title in Comic Sans because memes deserve dramaplt.show()— renders the final chart
Output:
A clean blue-toned pie chart showing meme popularity.
Going Further
Exploding Slices: Making One Piece Pop
explodes = [0, 0.1, 0, 0, 0, 0]
Sometimes, you want a slice to stand out — maybe it's the most important, or you just want it to grab attention. The explode parameter pulls out specific slices for emphasis.
Separate Legends, Clearer Vibes
plt.legend(memes, title="Memes", loc="upper left", bbox_to_anchor=(1, 1))
Instead of crowding the pie with labels inside slices, use plt.legend() to place them outside — neat and clean.
memes— the list of labelstitle="Memes"— adds a title to the legend boxloc="upper left"— positions the legend near the top-leftbbox_to_anchor=(1, 1)— pushes the legend outside the chart so it doesn't overlap
Donut Vibes with wedgeprops
plt.pie(data, wedgeprops=dict(width=0.7))
Want to turn your pie chart into a donut chart? wedgeprops lets you style the pie slices — setting width=0.7 hollows out the center.
When to Use Pie Charts
Avoid them when you have too many categories (it'll look like a pizza explosion), values are very close to each other (bar charts show differences better), or you need deep analytical insight.
Mini Project: Visualize Your Social Media Usage
Ever wondered how much time you spend scrolling through Instagram or watching YouTube Shorts? In this mini project, create a Python pie chart showing how you spend time on different social media apps.
Your Challenge
Build the chart yourself! Use any sample data — maybe the time you spent on Instagram, WhatsApp, YouTube, Twitter, and Snapchat last week.
Bonus Ideas
- Use real data from your phone's screen time tracker
- Add your own custom colors to the pie chart
- Animate or export the chart as an image