ProCodebaseProCodebase
  • ModusA Growth OS for your business, on WhatsAppKiosqSell more. Chase less.AI InterviewerAutomated screening & AI-led interviewsXperto AIAI prep companion for candidatesAI Tools HubResume builder, learning paths & more
  • Pre-Vetted DevelopersScreened, scored and ready to interviewAI-Native DevelopersSenior engineers on an hourly basis
  • Services
  • Features
  • Jobs
  • FAQs
Sign inBook a demo
  • Services
  • Features
  • Jobs
  • FAQs
Sign inBook a demo
ProCodebaseProCodebase

ProCodebase Technologies builds AI products for hiring and growth, and ships software for clients as a technical consultancy. We source, screen and deliver pre-vetted developers — so you only interview high-signal candidates.

Products

  • Modus
  • Kiosq
  • AI Interviewer
  • Xperto AI
  • AI Tools Hub

Hire & build

  • Pre-Vetted Developers
  • AI-Native Developers
  • Technical Consultancy
  • MVP Development
  • Features

Resources

  • Articles
  • Topics
  • Certifications
  • Collections
  • Jobs

Company

  • About Us
  • Contact Us
  • Book a Demo
  • FAQs

© 2026 ProCodebase Technologies. All rights reserved.

  • Privacy Policy
  • Terms & Conditions
  • Refund & Cancellation

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

  • Python Basics: Comprehensive Guide

    21/09/2024 · Python

  • Mastering Pandas: From Foundations to Advanced Data Engineering

    25/09/2024 · Python

  • Python with MongoDB: A Practical Guide

    08/11/2024 · Python

  • Python with Redis Cache

    08/11/2024 · Python

  • LangChain Mastery: From Basics to Advanced

    26/10/2024 · Python

Related articles

  • Mastering Data Visualization with Streamlit Charts in Python

    15/11/2024 · Python

  • Advanced Error Handling and Logging in Python

    15/01/2025 · Python

  • Introduction to Machine Learning and Scikit-learn

    15/11/2024 · Python

  • Exploring Hugging Face Model Hub and Community

    14/11/2024 · Python

  • Creating Your First Plot with Matplotlib

    05/10/2024 · Python

  • Managing Model Outputs and Predictions in Hugging Face Transformers

    14/11/2024 · Python

  • Mastering Django Models and Database Management

    26/10/2024 · Python

Popular category

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