Plot Pie Charts in Python with Matplotlib

📊 Data Viz Beginner

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

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:

Python
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

Output:

Pie chart output A clean blue-toned pie chart showing meme popularity.

Going Further

Exploding Slices: Making One Piece Pop

Python
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.

Exploded pie chart

Separate Legends, Clearer Vibes

Python
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.

Pie chart with legend

Donut Vibes with wedgeprops

Python
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.

Donut chart using wedgeprops

When to Use Pie Charts

Use pie charts when you're showing parts of a whole (budget, votes, proportions), there are fewer categories (ideally under 6), and you want a quick visual idea of relative sizes.

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

Once you're done, try applying the same logic to other areas of life — study time, expenses, or hobbies. This is just the start.