Most basic donut plot

logo of a chart:Doughnut

This post aims to describe how to plot a basic donut chart using matplotlib library.

The trick to plot a donut chart using matplotlib is to draw a pie plot and add a white circle in the middle of it. In order to draw a circle, you can use the Circle() function of matplotlib. The parameters passed to the function in the example below are:

  • (x,y) : center point of the circle
  • radius : radius of the circle
  • color : color of the circle

In the example, the add_artist() function is used to add the white circle on the axes of the pie chart. In order to get the current axes instance on the current figure, the gca() function is used and to get the current figure, the gcf() function is used.

# library
import matplotlib.pyplot as plt
 
# create data
size_of_groups=[12,11,3,30]
 
# Create a pie plot
plt.pie(size_of_groups)
#plt.show()
 
# add a white circle at the center
my_circle=plt.Circle( (0,0), 0.7, color='white')
p=plt.gcf()
p.gca().add_artist(my_circle)

# show the graph
plt.show()

Contact & Edit


👋 This document is a work by Yan Holtz. You can contribute on github, send me a feedback on twitter or subscribe to the newsletter to know when new examples are published! 🔥

This page is just a jupyter notebook, you can edit it here. Please help me making this website better 🙏!