logologo
  • Dashboard
  • Features
  • AI Tools
  • FAQs
  • Jobs
  • Modus
logologo

We source, screen & deliver pre-vetted developers—so you only interview high-signal candidates matched to your criteria.

Useful Links

  • Contact Us
  • Privacy Policy
  • Terms & Conditions
  • Refund & Cancellation
  • About Us

Resources

  • Certifications
  • Topics
  • Collections
  • Articles
  • Services

AI Tools

  • AI Interviewer
  • Xperto AI
  • Pre-Vetted Top Developers

Procodebase © 2025. All rights reserved.

Level Up Your Skills with Xperto-AI

A multi-AI agent platform that helps you level up your development skills and ace your interview preparation to secure your dream job.

Launch Xperto-AI

Mastering Pie Charts and Donut Plots with Matplotlib

author
Generated by
ProCodebase AI

05/10/2024

matplotlib

Sign in to read full article

Introduction to Circular Plots

When it comes to displaying parts of a whole, pie charts and donut plots are the go-to visualizations. They're intuitive, visually appealing, and can convey information at a glance. In this guide, we'll explore how to create these circular plots using Matplotlib, Python's powerful plotting library.

Setting Up Your Environment

Before we dive in, make sure you have Matplotlib installed. If not, you can easily install it using pip:

pip install matplotlib

Now, let's import the necessary libraries:

import matplotlib.pyplot as plt

Creating a Basic Pie Chart

Let's start with a simple pie chart. Imagine we're visualizing the favorite fruits in a survey:

fruits = ['Apples', 'Bananas', 'Cherries', 'Dates'] sizes = [35, 25, 20, 20] plt.pie(sizes, labels=fruits) plt.title("Favorite Fruits") plt.show()

This code will create a basic pie chart with four slices, each representing a fruit and its popularity.

Customizing Your Pie Chart

Now, let's make our pie chart more visually appealing and informative:

colors = ['#ff9999', '#66b3ff', '#99ff99', '#ffcc99'] explode = (0.1, 0, 0, 0) # "Explode" the first slice plt.pie(sizes, explode=explode, labels=fruits, colors=colors, autopct='%1.1f%%', startangle=90) plt.axis('equal') # Equal aspect ratio ensures that pie is drawn as a circle plt.title("Favorite Fruits") plt.show()

Here's what we've added:

  • colors: A list of custom colors for each slice
  • explode: Pulls out the first slice for emphasis
  • autopct: Adds percentage labels to each slice
  • startangle: Rotates the chart for better positioning

Creating a Donut Plot

Donut plots are essentially pie charts with a hole in the center. They can be more visually appealing and provide space for additional information. Let's transform our pie chart into a donut plot:

center_circle = plt.Circle((0,0), 0.70, fc='white') fig, ax = plt.subplots() ax.pie(sizes, explode=explode, labels=fruits, colors=colors, autopct='%1.1f%%', startangle=90) ax.add_artist(center_circle) plt.title("Favorite Fruits") plt.show()

The key here is creating a white circle and adding it to the center of our pie chart, creating the donut effect.

Adding Text to the Center

One advantage of donut plots is the ability to add text or data to the center:

fig, ax = plt.subplots() ax.pie(sizes, explode=explode, labels=fruits, colors=colors, autopct='%1.1f%%', startangle=90) ax.add_artist(center_circle) plt.text(0, 0, "Fruits", ha='center', va='center', fontsize=20) plt.title("Favorite Fruits") plt.show()

This adds the word "Fruits" to the center of our donut plot.

Multiple Pie Charts

Sometimes, you might want to compare multiple datasets side by side. Here's how you can create multiple pie charts in one figure:

fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(12,6)) ax1.pie(sizes, labels=fruits, autopct='%1.1f%%', startangle=90) ax1.set_title('Favorite Fruits') vegetables = ['Carrots', 'Broccoli', 'Spinach', 'Tomatoes'] veg_sizes = [30, 20, 25, 25] ax2.pie(veg_sizes, labels=vegetables, autopct='%1.1f%%', startangle=90) ax2.set_title('Favorite Vegetables') plt.show()

This creates two pie charts side by side, allowing for easy comparison between fruit and vegetable preferences.

Conclusion

Pie charts and donut plots are powerful tools for visualizing proportional data. With Matplotlib, you have the flexibility to create and customize these charts to suit your specific needs. Remember, while these charts are visually appealing, they're best used for displaying a small number of categories. For more complex data, consider other types of visualizations.

Happy plotting!

Popular Tags

matplotlibdata visualizationpie charts

Share now!

Like & Bookmark!

Related Collections

  • Seaborn: Data Visualization from Basics to Advanced

    06/10/2024 | Python

  • Mastering Scikit-learn from Basics to Advanced

    15/11/2024 | Python

  • Python with MongoDB: A Practical Guide

    08/11/2024 | Python

  • Mastering NLP with spaCy

    22/11/2024 | Python

  • Mastering NLTK for Natural Language Processing

    22/11/2024 | Python

Related Articles

  • Mastering Django Testing

    26/10/2024 | Python

  • Mastering Document Loaders and Text Splitting in LangChain

    26/10/2024 | Python

  • Unlocking Question Answering with Transformers in Python

    14/11/2024 | Python

  • Mastering Production Deployment Strategies for LangChain Applications

    26/10/2024 | Python

  • Mastering Prompt Engineering with LlamaIndex for Python Developers

    05/11/2024 | Python

  • Navigating the LLM Landscape

    26/10/2024 | Python

  • Mastering Dependency Injection in FastAPI

    15/10/2024 | Python

Popular Category

  • Python
  • Generative AI
  • Machine Learning
  • ReactJS
  • System Design