Creating Donut (Ring) Plots with Matplotlib in Python
This tutorial explains how to generate donut (ring) charts using Python's Matplotlib library, covering basic pie‑to‑donut conversion, custom colors, label positioning, background styling, annotation markers, and multi‑level donut visualizations with complete code examples.
Matplotlib can be used to create donut (ring) charts by first drawing a standard pie chart and then adding a white circle in the center, or by directly setting the wedgeprops parameter with a width value to create a hollow effect.
Basic donut plot
Example code:
# Remove warnings
import warnings
warnings.filterwarnings("ignore")
# Create data
size_of_groups = [12, 11, 3, 30]
# Plot pie chart
plt.pie(size_of_groups)
plt.axis('equal')
plt.show()
# Add white circle to make donut
my_circle = plt.Circle((0, 0), 0.7, color='white')
fig = plt.gcf()
fig.gca().add_artist(my_circle)
plt.axis('equal')
plt.show()Using wedgeprops for a simpler donut
plt.pie(size_of_groups, wedgeprops=dict(width=0.3, edgecolor='w'))
plt.axis('equal')
plt.show()Customizing colors
You can assign specific colors to each slice or let Matplotlib cycle through a list of colors. The colors argument accepts a list of color names or hex values, and wedgeprops can still control the ring width.
plt.pie(size, labels=names, colors=['red', 'green', 'blue', 'skyblue'], wedgeprops=dict(width=0.3, edgecolor='w'))
plt.axis('equal')
plt.show()Using external palettes (Palettable)
from palettable.colorbrewer.qualitative import Pastel1_7
plt.pie(size, labels=names, colors=Pastel1_7.hex_colors, wedgeprops=dict(width=0.3, edgecolor='w'))
plt.axis('equal')
plt.show()Custom label distance and color
plt.rcParams['text.color'] = 'red'
plt.pie(size, labels=names, labeldistance=0.85, wedgeprops=dict(width=0.3, edgecolor='w'))
plt.axis('equal')
plt.show()Custom wedge edge width and color
plt.pie(size, labels=names, wedgeprops={'linewidth':7, 'edgecolor':'white', 'width':0.3, 'edgecolor':'w'})
plt.axis('equal')
plt.show()Changing figure background and text color
fig = plt.figure()
fig.patch.set_facecolor('black')
plt.rcParams['text.color'] = 'white'
plt.pie(size, labels=names, wedgeprops=dict(width=0.3, edgecolor='w'))
plt.axis('equal')
plt.show()Adding annotation markers
By using ax.annotate with custom arrow and bbox properties, you can label each slice with detailed text and arrows.
# Example snippet (simplified)
ax.annotate('225 g flour', xy=(x, y), xytext=(1.35*np.sign(x), 1.4*y),
arrowprops=dict(arrowstyle='-'),
bbox=dict(boxstyle='square,pad=0.3', fc='white', ec='black', lw=0.72))
plt.show()Multi‑level donut plot (sub‑groups)
To visualize hierarchical data, draw two concentric pies: the outer ring for main groups and the inner ring for sub‑groups, adjusting the radius and width parameters.
# Outer ring
ax.pie(group_size, radius=1.3, labels=group_names,
colors=[a(0.6), b(0.6), c(0.6)], wedgeprops=dict(width=0.3, edgecolor='w'))
# Inner ring
ax.pie(subgroup_size, radius=1.3-0.3, labels=subgroup_names,
labeldistance=0.7,
colors=[a(0.5), a(0.4), a(0.3), b(0.5), b(0.4), c(0.6), c(0.5), c(0.4), c(0.3), c(0.2)],
wedgeprops=dict(width=0.4, edgecolor='w'))
plt.margins(0,0)
plt.show()These examples demonstrate how to build simple and complex donut charts, customize their appearance, and add informative annotations, providing a versatile tool for data visualization in Python.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Python Programming Learning Circle
A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
