Donut Chart using Matplotlib in Python

A Donut chart is a circular chart that shows the relative representation of data with a hole in the center, distinguishing it from a pie chart. Python's Matplotlib library provides simple methods to create customizable donut charts for visualizing categorical data proportions.

Installing Matplotlib

Matplotlib is a data visualization library in Python that allows you to create a wide range of charts. To install it, use the following command ?

pip install matplotlib

Basic Donut Chart

A donut chart is created using plt.pie() with the wedgeprops parameter to define the inner hole. Here's a simple example ?

import matplotlib.pyplot as plt

# Data
labels = ['Apples', 'Bananas', 'Grapes', 'Oranges']
sizes = [25, 30, 15, 30]

# Create figure
fig, ax = plt.subplots(figsize=(8, 8), subplot_kw=dict(aspect="equal"))

# Create donut chart
wedges, texts = ax.pie(sizes, labels=labels, wedgeprops=dict(width=0.5), 
                       startangle=90, autopct='%1.1f%%')

ax.set_title("Fruit Distribution", fontsize=16, fontweight='bold')
plt.show()

The output shows a donut chart with fruit distribution percentages.

Customizing Colors and Styling

You can enhance the donut chart with custom colors, edge styles, and better formatting ?

import matplotlib.pyplot as plt

# Data
labels = ['Apples', 'Bananas', 'Grapes', 'Oranges']
sizes = [25, 30, 15, 30]
colors = ['#ff6b6b', '#4ecdc4', '#45b7d1', '#f7b731']

# Create figure
fig, ax = plt.subplots(figsize=(10, 8), subplot_kw=dict(aspect="equal"))

# Create donut chart with custom styling
wedges, texts, autotexts = ax.pie(sizes, labels=labels, colors=colors,
                                  wedgeprops=dict(width=0.5, edgecolor='white', linewidth=2),
                                  startangle=90, autopct='%1.1f%%',
                                  textprops={'fontsize': 12, 'fontweight': 'bold'})

# Customize percentage text
for autotext in autotexts:
    autotext.set_color('white')
    autotext.set_fontweight('bold')

ax.set_title("Fruit Distribution", fontsize=18, fontweight='bold', pad=20)
plt.tight_layout()
plt.show()

This creates a more visually appealing donut chart with custom colors and white borders between segments.

Advanced Donut Chart with Legend

For better presentation, you can separate the legend from the chart and add more professional styling ?

import matplotlib.pyplot as plt

# Data
labels = ['Apples', 'Bananas', 'Grapes', 'Oranges']
sizes = [25, 30, 15, 30]
colors = ['#e74c3c', '#f39c12', '#9b59b6', '#3498db']

# Create figure
fig, ax = plt.subplots(figsize=(12, 8), subplot_kw=dict(aspect="equal"))

# Create donut chart
wedges, texts = ax.pie(sizes, wedgeprops=dict(width=0.4, edgecolor='white', linewidth=3),
                       startangle=90, colors=colors)

# Add center circle for donut effect
centre_circle = plt.Circle((0, 0), 0.6, fc='white', linewidth=3, edgecolor='gray')
fig.gca().add_artist(centre_circle)

# Add legend
ax.legend(wedges, [f'{label}: {size}%' for label, size in zip(labels, sizes)],
          title="Fruit Distribution",
          loc="center left",
          bbox_to_anchor=(1, 0, 0.5, 1),
          fontsize=12)

# Add title
ax.set_title("Fruit Sales Distribution", fontsize=20, fontweight='bold', pad=30)

# Add center text
ax.text(0, 0, 'Total\n100%', ha='center', va='center', 
        fontsize=16, fontweight='bold', color='gray')

plt.tight_layout()
plt.show()

This advanced example includes a legend, center text, and professional styling suitable for presentations.

Key Parameters

Important parameters for customizing donut charts ?

Parameter Description Example Value
wedgeprops Dictionary to style wedges (width, colors, edges) dict(width=0.5)
startangle Starting angle of first wedge in degrees 90
autopct Format string for percentage labels '%1.1f%%'
colors List of colors for each wedge ['red', 'blue']
explode Sequence to separate wedges from center (0, 0.1, 0, 0)

Conclusion

Donut charts in Matplotlib are created using plt.pie() with wedgeprops=dict(width=0.5) to create the center hole. They're excellent for showing proportional data with a modern, clean appearance that leaves space for additional information in the center.

Updated on: 2026-03-27T07:12:03+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements