0
0
Matplotlibdata~5 mins

Donut chart variation in Matplotlib

Choose your learning style9 modes available
Introduction

A donut chart helps show parts of a whole, like a pie chart, but with a hole in the middle. This makes it easier to read and looks nice.

Showing how different categories contribute to a total, like sales by product type.
Comparing proportions in a survey, such as favorite ice cream flavors.
Visualizing budget spending across departments with a clear center space.
Displaying election results by party share with a clean look.
Syntax
Matplotlib
import matplotlib.pyplot as plt

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

plt.pie(sizes, labels=labels, wedgeprops=dict(width=0.4))
plt.show()

The wedgeprops argument controls the thickness of the donut ring.

Setting width less than 1 creates the hole in the middle.

Examples
A simple donut chart with two parts and a thin ring.
Matplotlib
plt.pie([40, 60], labels=['Yes', 'No'], wedgeprops=dict(width=0.3))
plt.show()
Donut chart rotated to start at 90 degrees for better layout.
Matplotlib
plt.pie([10, 20, 30], labels=['X', 'Y', 'Z'], wedgeprops=dict(width=0.5), startangle=90)
plt.show()
Donut chart showing percentages inside each slice.
Matplotlib
plt.pie([25, 25, 25, 25], labels=['Q1', 'Q2', 'Q3', 'Q4'], wedgeprops=dict(width=0.4), autopct='%1.1f%%')
plt.show()
Sample Program

This program creates a donut chart showing fruit distribution with colors and percentages.

Matplotlib
import matplotlib.pyplot as plt

# Data for the chart
sizes = [15, 30, 45, 10]
labels = ['Apples', 'Bananas', 'Cherries', 'Dates']
colors = ['#ff9999','#66b3ff','#99ff99','#ffcc99']

# Create donut chart
plt.pie(sizes, labels=labels, colors=colors, autopct='%1.1f%%', startangle=140, wedgeprops=dict(width=0.5))

# Add a title
plt.title('Fruit Distribution')

# Show the plot
plt.show()
OutputSuccess
Important Notes

Donut charts are easier to read when the hole is not too small or too large.

Use autopct to add percentage labels inside slices.

Colors help distinguish categories clearly.

Summary

Donut charts are pie charts with a hole in the center.

Use wedgeprops=dict(width=...) to create the hole.

They help show parts of a whole in a clean, readable way.