0
0
MatplotlibHow-ToBeginner ยท 3 min read

How to Explode Slice in Pie Chart in Matplotlib

To explode a slice in a pie chart using matplotlib, use the explode parameter in the pie() function. Pass a list of values where each value represents the fraction of the radius to offset each slice outward, with zero meaning no explosion.
๐Ÿ“

Syntax

The explode parameter in matplotlib.pyplot.pie() takes a list or tuple of floats. Each float corresponds to a slice and defines how far that slice is offset from the center.

  • explode=[0, 0.1, 0, 0] means only the second slice is exploded outward by 10% of the radius.
  • The length of the explode list must match the number of slices.
python
matplotlib.pyplot.pie(x, explode=None, labels=None, autopct=None, shadow=False, startangle=0, radius=1, counterclock=True, wedgeprops=None, textprops=None, center=(0, 0), frame=False, rotatelabels=False, pctdistance=0.6, labeldistance=1.1, normalize=True)
๐Ÿ’ป

Example

This example shows how to explode the first slice of a pie chart to highlight it.

python
import matplotlib.pyplot as plt

sizes = [30, 20, 25, 25]
labels = ['Apples', 'Bananas', 'Cherries', 'Dates']
explode = [0.1, 0, 0, 0]  # Explode first slice

plt.pie(sizes, labels=labels, explode=explode, autopct='%1.1f%%', shadow=True, startangle=90)
plt.title('Fruit Distribution with Exploded Slice')
plt.show()
Output
A pie chart with four slices labeled Apples, Bananas, Cherries, and Dates. The Apples slice is slightly separated from the center, visually exploded.
โš ๏ธ

Common Pitfalls

  • Not matching the length of the explode list to the number of slices causes an error.
  • Using values too large in explode can push slices too far, making the chart look odd.
  • For no explosion, use zeros for all slices.
python
import matplotlib.pyplot as plt

sizes = [40, 30, 20, 10]
labels = ['A', 'B', 'C', 'D']

# Wrong: explode list length does not match sizes
# explode = [0.1, 0.2]  # This will cause an error

# Correct:
explode = [0.1, 0, 0, 0]

plt.pie(sizes, labels=labels, explode=explode, autopct='%1.1f%%')
plt.show()
Output
A pie chart with four slices labeled A, B, C, and D. The first slice (A) is exploded outward slightly.
๐Ÿ“Š

Quick Reference

Use the explode parameter to offset slices in a pie chart. Values are fractions of the radius.

  • 0: no explosion
  • 0.1: 10% offset outward
  • Length of list = number of slices
ParameterDescriptionExample
explodeList of floats to offset slices[0, 0.1, 0, 0]
labelsList of slice labels['A', 'B', 'C', 'D']
autopctFormat string for percent labels'%1.1f%%'
shadowAdd shadow effectTrue or False
startangleRotate start angle in degrees90
โœ…

Key Takeaways

Use the explode parameter in plt.pie() to offset slices outward.
The explode list must match the number of slices exactly.
Values in explode represent fraction of radius to offset each slice.
Small explode values (e.g., 0.1) create a clear highlight without distortion.
Zero means no explosion; use zeros for slices you don't want to offset.