Pie charts might not be the most advanced data visualization tool, but they serve a simple purpose: to show proportions at a glance. Whether you’re analyzing sales figures, poll results, or survey outcomes, a clean pie chart communicates the point effectively. In Python, the Matplotlib library makes it straightforward to generate and customize these charts to suit your needs.
Matplotlib, one of the most stable plotting libraries in Python, offers a simple function called pie()
to draw pie charts. Before diving into code, ensure Matplotlib is installed:
pip install matplotlib
Here’s how to create a basic pie chart:
import matplotlib.pyplot as plt
labels = ['Apples', 'Bananas', 'Cherries', 'Dates']
sizes = [30, 25, 25, 20]
plt.pie(sizes, labels=labels)
plt.title('Fruit Sales Distribution')
plt.show()
This generates a default pie chart with labeled slices. For added clarity, you can display percentages directly on the chart:
plt.pie(sizes, labels=labels, autopct='%1.1f%%')
The autopct
parameter formats the percentages to one decimal place, ensuring quick readability.
Once you have a basic chart, you can customize its appearance for clarity and emphasis. Use the colors
parameter to change slice colors:
colors = ['#ff9999', '#66b3ff', '#99ff99', '#ffcc99']
plt.pie(sizes, labels=labels, colors=colors, autopct='%1.1f%%')
To highlight a specific slice, such as emphasizing ‘Apples’, use the explode
parameter:
explode = (0.1, 0, 0, 0) # Emphasize the first slice
plt.pie(sizes, explode=explode, labels=labels, colors=colors, autopct='%1.1f%%')
Adding a shadow or adjusting the start angle can also enhance readability:
plt.pie(sizes, labels=labels, autopct='%1.1f%%', startangle=90, shadow=True)
For a donut-style chart, add a circle at the center:
wedges, texts, autotexts = plt.pie(sizes, labels=labels, autopct='%1.1f%%')
centre_circle = plt.Circle((0, 0), 0.70, fc='white')
fig = plt.gcf()
fig.gca().add_artist(centre_circle)
Pie charts are often generated from datasets rather than hardcoded values. Using Pandas, you can create a pie chart from a dataset:
import pandas as pd
data = {
'Fruit': ['Apples', 'Bananas', 'Cherries', 'Dates'],
'Sales': [300, 250, 250, 200]
}
df = pd.DataFrame(data)
plt.pie(df['Sales'], labels=df['Fruit'], autopct='%1.1f%%')
plt.title('Sales Distribution by Fruit')
plt.show()
For integration into dashboards or automated reports, save your chart instead of displaying it:
plt.savefig('pie_chart.png')
When creating data visualizations, selecting the right chart type is crucial. Pie charts are excellent for simple breakdowns with a few categories, but as segments increase, readability declines. In such cases, bar charts offer better clarity. For trends over time, line charts provide more insight. While Matplotlib’s pie chart capabilities are robust, sometimes switching to a bar or line chart enhances clarity.
Pie charts are not about showing everything—they’re about emphasizing the essentials. When applied effectively, they bring out stories that might be buried in a table or lost in a bar chart. With Matplotlib, crafting a Python pie chart is quick and doesn’t compromise on style or readability. Whether it’s a report, business review, or dashboard summary, a well-placed pie chart delivers your message cleanly. Keep it simple, keep it readable, and let the proportions do the talking. That’s the power of effective data visualization.
Discover the essential differences between class and instance attributes in Python. Learn how each operates, when to apply them, and their impact on Python classes.
Ready to dive into Python? This guide covers the core concepts you need to understand, helpful resources, and project ideas to start using Python effectively. Perfect for both beginners and those brushing up.
Hyundai creates new brand to focus on the future of software-defined vehicles, transforming how cars adapt, connect, and evolve through intelligent software innovation.
Discover how Deloitte's Zora AI is reshaping enterprise automation and intelligent decision-making at Nvidia GTC 2025.
Discover how Nvidia, Google, and Disney's partnership at GTC aims to revolutionize robot AI infrastructure, enhancing machine learning and movement in real-world scenarios.
What is Nvidia's new AI Factory Platform, and how is it redefining AI reasoning? Here's how GTC 2025 set a new direction for intelligent computing.
Can talking cars become the new normal? A self-driving taxi prototype is testing a conversational AI agent that goes beyond basic commands—here's how it works and why it matters.
Hyundai is investing $21 billion in the U.S. to enhance electric vehicle production, modernize facilities, and drive innovation, creating thousands of skilled jobs and supporting sustainable mobility.
An AI startup hosted a hackathon to test smart city tools in simulated urban conditions, uncovering insights, creative ideas, and practical improvements for more inclusive cities.
Researchers fine-tune billion-parameter AI models to adapt them for specific, real-world tasks. Learn how fine-tuning techniques make these massive systems efficient, reliable, and practical for healthcare, law, and beyond.
How AI is shaping the 2025 Masters Tournament with IBM’s enhanced features and how Meta’s Llama 4 models are redefining open-source innovation.
Discover how next-generation technology is redefining NFL stadiums with AI-powered systems that enhance crowd flow, fan experience, and operational efficiency.
Gartner forecasts task-specific AI will outperform general AI by 2027, driven by its precision and practicality. Discover the reasons behind this shift and its impact on the future of artificial intelligence.
Hugging Face has entered the humanoid robots market following its acquisition of a robotics firm, blending advanced AI with lifelike machines for homes, education, and healthcare.