0
0
Matplotlibdata~5 mins

Exploding slices in Matplotlib

Choose your learning style9 modes available
Introduction

Exploding slices help highlight parts of a pie chart by pulling them out slightly. This makes it easier to see and focus on important sections.

You want to emphasize a particular category in a pie chart.
You need to make one slice stand out for a presentation or report.
You want to visually separate slices to improve readability.
You are comparing parts of a whole and want to draw attention to differences.
Syntax
Matplotlib
plt.pie(data, explode=[0, 0.1, 0, 0], labels=labels)

The explode parameter takes a list of numbers.

Each number shows how far to pull out the slice from the center.

Examples
This pulls out the second slice slightly to highlight it.
Matplotlib
plt.pie([30, 20, 50], explode=[0, 0.1, 0], labels=['A', 'B', 'C'])
This pulls out the first slice more noticeably.
Matplotlib
plt.pie([10, 40, 50], explode=[0.2, 0, 0], labels=['X', 'Y', 'Z'])
Sample Program

This code creates a pie chart of fruit amounts. The 'Bananas' slice is pulled out slightly to highlight it. Percentages and a shadow are added for clarity.

Matplotlib
import matplotlib.pyplot as plt

# Data and labels
sizes = [15, 30, 45, 10]
labels = ['Apples', 'Bananas', 'Cherries', 'Dates']

# Explode the second slice (Bananas)
explode = [0, 0.1, 0, 0]

# Create pie chart
plt.pie(sizes, explode=explode, labels=labels, autopct='%1.1f%%', shadow=True, startangle=90)
plt.title('Fruit Distribution with Exploded Slice')
plt.show()
OutputSuccess
Important Notes

Explode values are fractions of the radius, so 0.1 means 10% away from the center.

Use explode sparingly to avoid cluttering the chart.

Summary

Exploding slices pull parts of a pie chart outward to highlight them.

Use the explode parameter with a list matching the number of slices.

This technique improves focus and readability in pie charts.