Donut Charts in Python – An Introduction

Filed Under: Python Modules
Donut Plots Python

In this tutorial, we will know what Donut charts are and how to create them in the Python programming language.

Introduction to Donut Plots in Python

Donut or Doughnut Charts are special Pie charts with the only difference of having a Blank Circle at the center of the chart. All the functionality of a pie chart is also available on the donut chart. By default, the ring size is 30% of the whole ring radius, which means the center hole occupies 70% of the whole chart.

Donut Vs Pie Charts Demonstration
Donut Vs Pie Charts Demonstration

Each piece of the outer ring represents a proportion of the whole Dataset. The blank circle at the center can be helpful to show additional information about the data.

Code Implementation for Donut Charts in Python

The whole chart can be built using the infamous matplotlib library.

Also Read: Python Matplotlib

The Donut plot is similar to a pie chart, except it has a hole in the middle similar to a donut. Hence, it is built the same way pie charts are built with some additional commands to get the blank donut hole at the center of the chart.

1. Importing Modules

import numpy as np
import matplotlib.pyplot as plt
plt.style.use('ggplot')

2. Creating Random Dataset

We will generate 20 random numbers in a certain range. For now, we have kept the range between 0 and 100. You can change the number of data points and the range according to you.

DATA = np.random.randint(0, 100, 20)
print(DATA)

3. Plotting a Basic Pie Chart

Before plotting the Donut plot, let’s plot the basic pie chart for the data we have from the previous step. The code for the same is shown below.

plt.figure(figsize=(10,10),facecolor='w')
plt.pie(DATA,wedgeprops={"edgecolor":"black"})
plt.title("A Simple Pie Chart for the Data",bbox={'facecolor':'0.5', 'pad':5})
plt.show()
Pie Chart Before Donut Chart
Pie Chart Before Donut Chart

4. Creating a white circle and it to the Pie Chart

We will first try to create a clean white circle using the code below. For now, we will store the circular figure in a variable. Then we will use the gcf and gca to get the current axes and the add_artist method of adding a figure to the pie chart.

central_circle = plt.Circle((0, 0), 0.7, color='white')
p=plt.gcf()
p.gca().add_artist(central_circle)

5. Final Python Code for Plotting Donut Charts

import numpy as np
import matplotlib.pyplot as plt
plt.style.use('ggplot')

DATA = np.random.randint(0, 100, 20)
print(DATA)

plt.figure(figsize=(10,10),facecolor='w')
plt.pie(DATA,wedgeprops={"edgecolor":"black"})
plt.title("The Donut Chart for the Data",bbox={'facecolor':'0.5', 'pad':5})

central_circle = plt.Circle( (0,0), 0.7, color='white')
p=plt.gcf()
p.gca().add_artist(central_circle)

plt.show()
Donut Chart Plot Output
Donut Chart Plot Output

Donut Plots are Easy to plot and are user-friendly. One can easily add or remove data and even adjust the data for the donut plot. Along with this, as some extra space is available, you can add some more information in the center as well.

But Donut charts can’t determine the exact value of a category unless we annotate them and hence can lead to misinterpretations. Also, the donut charts can’t display negative values.

Conclusion

I hope you liked this tutorial on what donut plots are and how to create them with Python programming language.

Keep reading to learn more!

Also Read: Python Plotly Tutorial

close
Generic selectors
Exact matches only
Search in title
Search in content