Explode multiple slices of pie together in matplotlib
Categories:
Exploding Multiple Slices in Matplotlib Pie Charts

Learn how to visually emphasize multiple segments of a pie chart in Matplotlib by 'exploding' them, enhancing data presentation and insights.
Matplotlib's pie()
function is a versatile tool for visualizing proportional data. While exploding a single slice to highlight it is straightforward, exploding multiple slices simultaneously requires a slightly different approach. This article will guide you through the process, explaining the explode
parameter and demonstrating how to apply it effectively to multiple segments of your pie chart.
Understanding the explode
Parameter
The explode
parameter in Matplotlib's plt.pie()
function is used to offset a slice from the center of the pie. It takes an array-like object (e.g., a list or tuple) of the same length as your data. Each value in this array represents the fraction of the radius by which to 'explode' the corresponding slice. A value of 0
means the slice remains attached to the center, while a positive value (e.g., 0.1
) will move it outwards. The larger the value, the further the slice will be offset.
import matplotlib.pyplot as plt
# Sample data
labels = ['Apples', 'Bananas', 'Cherries', 'Dates']
sizes = [15, 30, 45, 10]
# Explode only 'Bananas'
explode_single = [0, 0.1, 0, 0]
fig1, ax1 = plt.subplots()
ax1.pie(sizes, explode=explode_single, labels=labels, autopct='%1.1f%%',
shadow=True, startangle=90)
ax1.axis('equal') # Equal aspect ratio ensures that pie is drawn as a circle.
plt.title('Pie Chart with Single Exploded Slice')
plt.show()
Basic example of exploding a single slice in a Matplotlib pie chart.
Exploding Multiple Slices
To explode multiple slices, you simply provide a list or array to the explode
parameter where each element corresponds to a slice in your data. Set a non-zero value for each slice you wish to explode. The magnitude of this value determines how far out the slice will be pushed. This allows for fine-grained control over the visual emphasis of different data points.
pie title Pie Chart Data Flow "Data Input" : 100 "Explode Array" : 100 "Matplotlib pie()" : 100 "Rendered Pie Chart" : 100
Conceptual flow of data and the explode
array into Matplotlib's pie function.
import matplotlib.pyplot as plt
# Sample data
labels = ['North', 'South', 'East', 'West', 'Central']
sizes = [25, 20, 15, 30, 10]
# Explode 'North', 'East', and 'Central' slices
# The order must match the order of 'sizes' and 'labels'
explode_multiple = [0.1, 0, 0.2, 0, 0.15] # North, East, Central are exploded
fig2, ax2 = plt.subplots(figsize=(8, 8))
ax2.pie(sizes, explode=explode_multiple, labels=labels, autopct='%1.1f%%',
shadow=True, startangle=140)
ax2.axis('equal') # Equal aspect ratio ensures that pie is drawn as a circle.
plt.title('Pie Chart with Multiple Exploded Slices')
plt.show()
Python code demonstrating how to explode multiple slices in a Matplotlib pie chart.
explode
values, aim for consistency if you want to emphasize multiple slices equally. Varying the values can create a visual hierarchy, drawing more attention to slices with larger explode
factors.Dynamic Explode Based on Conditions
You might want to explode slices based on certain conditions, such as values exceeding a threshold or specific categories. This can be achieved by programmatically generating the explode
list.
import matplotlib.pyplot as plt
# Sample data
labels = ['Category A', 'Category B', 'Category C', 'Category D', 'Category E']
sizes = [12, 28, 35, 10, 15]
# Define a threshold for exploding slices
threshold = 20
# Generate explode list dynamically
# Explode slices where size is greater than the threshold
explode_dynamic = [0.1 if size > threshold else 0 for size in sizes]
fig3, ax3 = plt.subplots(figsize=(8, 8))
ax3.pie(sizes, explode=explode_dynamic, labels=labels, autopct='%1.1f%%',
shadow=True, startangle=90)
ax3.axis('equal')
plt.title(f'Pie Chart with Slices > {threshold}% Exploded')
plt.show()
Dynamically exploding slices based on a size threshold.
explode
values can make the pie chart difficult to read and interpret. Use explode
judiciously to maintain clarity.