Radially displace pir chart wedge in Matplotlib
×


Radially displace pir chart wedge in Matplotlib

766

Introduction

Pie charts are useful for displaying data proportions visually, but sometimes you need to emphasize a specific slice to draw the viewer's attention. Matplotlib provides a simple way to achieve this using radial displacement — also known as "exploding" a wedge. In this guide, you’ll learn how to radially displace wedges in a pie chart using the explode parameter in Matplotlib.

Understanding the explode Parameter

The explode argument in plt.pie() allows you to pull one or more wedges outward from the center of the pie. It takes a list of values, where each number corresponds to the radial offset of a wedge. A value of 0 means no displacement, while higher values move the wedge further out.

Basic Example: Highlighting One Slice

Let’s start with a simple example where we displace just one wedge to highlight it.

import matplotlib.pyplot as plt

# Data setup
sizes = [40, 30, 20, 10]
labels = ['A', 'B', 'C', 'D']
explode = [0.1, 0, 0, 0]  # Displace the first wedge

# Create pie chart
plt.pie(sizes, labels=labels, explode=explode, autopct='%1.1f%%', startangle=90)
plt.title('Pie Chart with One Wedge Displaced')
plt.show()
In this case, only the 'A' slice is moved slightly outwards to make it stand out from the rest.

Displacing Multiple Wedges

You can also displace more than one wedge by specifying different values in the explode list. This can be useful when you want to highlight multiple categories.

import matplotlib.pyplot as plt

sizes = [25, 25, 25, 25]
labels = ['Q1', 'Q2', 'Q3', 'Q4']
explode = [0.05, 0.1, 0.05, 0.1]  # Displace all wedges differently

plt.pie(sizes, labels=labels, explode=explode, autopct='%1.1f%%', startangle=140)
plt.title('Pie Chart with Multiple Displaced Wedges')
plt.show()
Here, all four wedges are slightly offset, with alternating displacement values for added visual balance.

Combining Displacement with Styling

Radial displacement can be even more effective when combined with custom colors, borders, or shadows. This enhances readability and makes the chart more visually engaging.

import matplotlib.pyplot as plt

sizes = [45, 30, 15, 10]
labels = ['Alpha', 'Beta', 'Gamma', 'Delta']
colors = ['skyblue', 'lightgreen', 'salmon', 'orange']
explode = [0.1, 0.05, 0, 0.1]

plt.pie(
    sizes,
    labels=labels,
    explode=explode,
    colors=colors,
    autopct='%1.1f%%',
    startangle=100,
    shadow=True,
    wedgeprops={'edgecolor': 'gray'}
)
plt.title('Styled Pie Chart with Radially Displaced Wedges')
plt.show()
This version uses colors, borders, shadows, and radial displacement to make the chart more informative and visually appealing.

Conclusion

Radially displacing pie chart wedges in Matplotlib is a simple yet effective way to highlight specific data segments. By using the explode parameter, you can easily emphasize important values in your data, whether it's a single wedge or several. Combine this technique with styling options like custom colors, shadows, and edge borders to make your pie charts both functional and beautiful.


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