Working with data visualization in Python often requires us to show hierarchical relationships. One of my favorite ways to represent this is through a nested pie chart.
I have spent over a decade building complex dashboards using Python. I found that a standard pie chart often fails when you need to show a “breakdown of a breakdown.”
In this tutorial, I will show you exactly how to build a Python Matplotlib nested pie chart with labels. We will use real-world data to make this practical.
What is a Nested Pie Chart in Python Matplotlib?
A nested pie chart, often called a multi-level donut chart, consists of an outer ring and an inner circle (or multiple rings).
The inner ring usually represents a broad category. The outer ring then breaks that category down into specific sub-categories.
I prefer using these when I want to compare the total share of a group while simultaneously showing the individual contributors.
Method 1: Create a Basic Python Matplotlib Nested Pie Chart
When I first started using Matplotlib, I realized there isn’t a direct plt.nested_pie() function. Instead, we layer two pie charts on top of each other.
In this example, we will look at the USA Smartphone Market Share. We will categorize them by Operating System and then by specific Brands.
import matplotlib.pyplot as plt
import numpy as np
# Data for the USA Smartphone Market
# Inner Ring: OS Type
# Outer Ring: Specific Brands
os_labels = ['iOS', 'Android']
os_counts = [55, 45]
brand_labels = ['Apple', 'Samsung', 'Google', 'Motorola', 'Others']
brand_counts = [55, 28, 7, 5, 5]
# Defining the colors for our Python plot
colors_os = ['#ff9999', '#66b3ff']
colors_brands = ['#ff6666', '#3399ff', '#1e90ff', '#00bfff', '#87cefa']
# Creating the figure
fig, ax = plt.subplots(figsize=(10, 7))
# Outer Pie Chart (Brands)
ax.pie(brand_counts, labels=brand_labels, radius=1,
colors=colors_brands, wedgeprops=dict(width=0.3, edgecolor='w'))
# Inner Pie Chart (OS)
ax.pie(os_counts, labels=os_labels, radius=0.7,
colors=colors_os, labeldistance=0.4, wedgeprops=dict(width=0.3, edgecolor='w'))
ax.set(aspect="equal", title="USA Smartphone Market Share: OS vs Brand")
plt.show()You can refer to the screenshot below to see the output.

The wedgeprops parameter is the secret sauce here. By setting a width, I turned the pie into a ring, creating that clean, nested look.
Method 2: Add Percentage Labels to Nested Pie Charts
Adding text labels to a nested chart can get messy quickly. I often find that users struggle to read overlapping text.
To fix this, we can use the autopct parameter in Python Matplotlib. This automatically calculates the percentage for us.
Let’s look at the USA Energy Consumption by Sector (Residential vs. Commercial) and then by Source (Renewable vs. Non-Renewable).
import matplotlib.pyplot as plt
# USA Energy Consumption Data (Hypothetical for demonstration)
group_names = ['Renewable', 'Non-Renewable']
group_size = [20, 80]
subgroup_names = ['Solar', 'Wind', 'Coal', 'Gas', 'Nuclear']
subgroup_size = [10, 10, 30, 30, 20]
# Colors
a, b, c, d, e = [plt.cm.Blues, plt.cm.Reds, plt.cm.Greens, plt.cm.Purples, plt.cm.YlOrBr]
fig, ax = plt.subplots()
ax.axis('equal')
# Outer Ring
mypie, _ = ax.pie(
group_size,
radius=1.3,
labels=group_names,
colors=[a(0.6), b(0.6)]
)
# Inner Ring
mypie2, _, _ = ax.pie(
subgroup_size,
radius=1.0,
labels=subgroup_names,
labeldistance=0.7,
autopct='%1.1f%%',
colors=[a(0.4), a(0.2), b(0.4), b(0.2), c(0.4)]
)
plt.setp(mypie, width=0.3, edgecolor='white')
plt.setp(mypie2, width=0.3, edgecolor='white')
plt.title("USA Energy Consumption Breakdown")
plt.show()You can refer to the screenshot below to see the output.

I use plt.setp to modify the width of the wedges after creation. This keeps the code organized and allows for easier adjustments to the “donut” thickness.
Method 3: Use a Legend for Python Matplotlib Nested Pie Labels
Sometimes, the outer ring has too many small slices. Putting labels on every slice makes the Python chart look cluttered.
In my professional projects, I prefer to move the labels to a legend. This keeps the visual clean while still providing all the necessary data.
Let’s visualize the USA Beverage Market share, focusing on Soda Brands vs. Health Drinks.
import matplotlib.pyplot as plt
# Data
categories = ['Soda', 'Healthy']
cat_totals = [60, 40]
sub_categories = ['Coke', 'Pepsi', 'Dr. Pepper', 'Water', 'Juice']
sub_totals = [30, 20, 10, 25, 15]
fig, ax = plt.subplots(figsize=(8, 8))
# Outer Chart
outer = ax.pie(cat_totals, radius=1, colors=['#d62728', '#2ca02c'],
wedgeprops=dict(width=0.3, edgecolor='w'))
# Inner Chart
inner = ax.pie(sub_totals, radius=0.7, colors=['#ff9896', '#ff7f0e', '#dbdb8d', '#98df8a', '#c7c7c7'],
wedgeprops=dict(width=0.3, edgecolor='w'))
# Adding a Legend
ax.legend(sub_categories, title="Product List", loc="center left", bbox_to_anchor=(1, 0, 0.5, 1))
plt.title("USA Beverage Market Analysis")
plt.show()You can refer to the screenshot below to see the output.

I used bbox_to_anchor to place the legend outside the actual plot area. This is a common trick I use to prevent the legend from overlapping the chart.
Important Considerations for Nested Charts
When you build these in Python, keep the “Data-to-Ink” ratio in mind. Do not add too many rings.
I find that two rings are perfect. Three rings are okay if the data is very distinct. Anything more than that becomes a “Sunburst chart,” which requires different Python logic.
Also, always ensure your colors provide enough contrast. Using a color map like plt.cm.Paired can help keep related categories visually linked.
Troubleshoot Common Python Matplotlib Issues
One issue I frequently see is the “Oval Pie Chart.” This happens because the default aspect ratio of the figure is rectangular.
Always include ax.axis(‘equal’) or ax.set(aspect=”equal”). This forces Python to draw a perfect circle.
Another issue is label overlap. If your labels are clashing, try reducing the fontsize or using the pctdistance parameter to move the percentages closer to the center.
Conclusion
Creating a Python Matplotlib nested pie chart with labels is a great way to show hierarchical data. It takes a bit more effort than a standard pie chart, but the result is much more professional.
I have used these charts in many executive reports to show how specific subdivisions contribute to the overall company health in the USA market.
Try experimenting with different color palettes and radii to find the look that best fits your specific data story.
You may read:
- Matplotlib Pie Chart Autopct to Format Percentages
- Python Matplotlib Pie Chart Hatch
- Python Matplotlib Pie Chart Explode and Shadow Effects
- Python Matplotlib Multiple Pie Charts

I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.