logologo
  • AI Tools

    DB Query GeneratorMock InterviewResume BuilderLearning Path GeneratorCheatsheet GeneratorAgentic Prompt GeneratorCompany ResearchCover Letter Generator
  • XpertoAI
  • MVP Ready
  • Resources

    CertificationsTopicsExpertsCollectionsArticlesQuestionsVideosJobs
logologo

Elevate Your Coding with our comprehensive articles and niche collections.

Useful Links

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

Resources

  • Xperto-AI
  • Certifications
  • Python
  • GenAI
  • Machine Learning

Interviews

  • DSA
  • System Design
  • Design Patterns
  • Frontend System Design
  • ReactJS

Procodebase © 2024. 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

  • Mastering Hugging Face Transformers

    14/11/2024 | Python

  • Mastering LangGraph: Stateful, Orchestration Framework

    17/11/2024 | Python

  • Python with Redis Cache

    08/11/2024 | Python

  • Python Basics: Comprehensive Guide

    21/09/2024 | Python

  • Python Advanced Mastery: Beyond the Basics

    13/01/2025 | Python

Related Articles

  • Adding Interactivity to Streamlit Apps

    15/11/2024 | Python

  • Advanced Regular Expressions in Python

    13/01/2025 | Python

  • Mastering Django ORM

    26/10/2024 | Python

  • Streamlining Data Ingestion

    05/11/2024 | Python

  • Unlocking the Power of Statistical Visualizations with Matplotlib

    05/10/2024 | Python

  • Mastering URL Routing and Patterns in Django

    26/10/2024 | Python

  • Getting Started with Matplotlib

    05/10/2024 | Python

Popular Category

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