Plot a Pie Chart in Python using Matplotlib
×


Plot a Pie Chart in Python using Matplotlib

1084

Introduction

Pie charts are a classic way to visualize proportions within a dataset. They help you quickly see how different parts contribute to a whole. In Python, Matplotlib makes it easy to create pie charts with a variety of customization options. Let’s walk through how to plot a pie chart using Matplotlib.

Basic Pie Chart Creation

To start, you need some data representing categories and their corresponding values. The plt.pie() function takes this data and generates a pie chart.

import matplotlib.pyplot as plt

# Sample data
sizes = [30, 20, 45, 5]
labels = ['Apples', 'Bananas', 'Cherries', 'Dates']

plt.pie(sizes, labels=labels)
plt.title('Basic Pie Chart')
plt.show()

This will create a simple pie chart where each slice corresponds to the percentage of each fruit category.

Adding Exploded Slices to Emphasize

You can highlight specific slices by “exploding” them — that is, slightly pulling them away from the center to draw attention.

import matplotlib.pyplot as plt

sizes = [30, 20, 45, 5]
labels = ['Apples', 'Bananas', 'Cherries', 'Dates']
explode = (0, 0.1, 0, 0)  # Only 'Bananas' slice is exploded

plt.pie(sizes, labels=labels, explode=explode, shadow=True, startangle=140)
plt.title('Pie Chart with Exploded Slice')
plt.show()

Here, the 'Bananas' slice is separated a bit, and the chart has a shadow for depth, with slices starting from a 140-degree angle.

Displaying Percentage Values on the Pie

To make the chart more informative, you can display the percentage each slice represents using the autopct parameter.

import matplotlib.pyplot as plt

sizes = [30, 20, 45, 5]
labels = ['Apples', 'Bananas', 'Cherries', 'Dates']

plt.pie(sizes, labels=labels, autopct='%1.1f%%', startangle=90)
plt.title('Pie Chart with Percentage Labels')
plt.show()

The autopct='%1.1f%%' formats the percentages with one decimal place.

Customizing Colors and Adding a Legend

You can customize slice colors by passing a list of colors, and add a legend for clarity.

import matplotlib.pyplot as plt

sizes = [30, 20, 45, 5]
labels = ['Apples', 'Bananas', 'Cherries', 'Dates']
colors = ['gold', 'yellowgreen', 'lightcoral', 'lightskyblue']

plt.pie(sizes, labels=labels, colors=colors, autopct='%1.1f%%', startangle=140)
plt.legend(labels, loc='upper right')
plt.title('Pie Chart with Custom Colors and Legend')
plt.show()

Wrapping Up

Creating pie charts with Matplotlib is intuitive and flexible. By tweaking parameters like explode, autopct, and colors, you can build charts that effectively communicate your data’s story. Experiment with these options to find the style that best fits your needs.


If you’re passionate about building a successful blogging website, check out this helpful guide at Coding Tag – How to Start a Successful Blog. It offers practical steps and expert tips to kickstart your blogging journey!

For dedicated UPSC exam preparation, we highly recommend visiting www.iasmania.com. It offers well-structured resources, current affairs, and subject-wise notes tailored specifically for aspirants. Start your journey today!



Best WordPress Hosting


Share:


Discount Coupons

Unlimited Video Generation

Best Platform to generate videos

Search and buy from Namecheap

Secure Domain for a Minimum Price



Leave a Reply


Comments
    Waiting for your comments

Coding Tag WhatsApp Chat