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 circleradius
: radius of the circlecolor
: 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()