Delightful Figures in Python Series: Donut Plot
By: Brinnae Bent
Tags: Donut Chart, Data Science, Python, Data Analysis, Data Analytics

I wanted to put together a series to guide Python-ers through generating delightful figures. Delightful figures can help you effectively tell your story, add credibility to your data analysis, and give your papers/articles/blogs a pop of fun.

We are going to start with one of my personal favorites- the donut plot! (And it is not just my favorite because it is also a delicious breakfast). While the pie chart has been through the ringer of bad press (due to our brain having a hard time comparing sizes of angles), the donut plot has emerged as an excellent alternative. I highly recommend this blog, which compares donut and pie charts.

Getting Started

Some basic assumptions before we get started: I am assuming you have, at minimum, a basic understanding of python and you have a working knowledge of pandas and matplotlib.

The dataset

This tutorial is BYODS (Bring your own data set), but if you want to follow along with the dataset I am using, I am using the Kaggle UFO sightings dataset, freely available here.

Make sure you have imported pandas, and matplotlib before starting:

First, let’s read the .csv file into Python and examine the data:

If you are following along with the UFO dataset, the first few lines of the dataframe should look like this:

I am interested in seeing what shapes are commonly seen in UFO sightings, which is the pandas dataframe column ‘shape’.

Formatting our data for the donut plot

First, we will pull out the column ‘shapes’ from the dataframe. We will count the values, normalize them, and multiply them by 100 to get the percent that each shape is encountered in a UFO sighting.

We will pull out the ‘Keys’ and ‘Percent’ as lists for easy input into our donut plot functions.

Building the donut plot

Create a matplotlib figure and update parameters:

We are going to set the three main components of our donut plot. We are going to set labels = Keys (this is also where you can input a list of your own labels, if necessary). We are going to set the wedge sizes, sizes = Percent. Then we are going to add a list of colors for the donut plot. You are welcome to use the default here, but I prefer to set specific colors based on the application. I highly recommend browsing the matplotlib named colors guide to put together your own color palette. The right colors will make a plot pop!

Now, it’s time to use the matplotlib pie function to plot a pie chart (I thought you said we were doing a donut plot! In time, my young Padawan). I have added the argument autopct (optional), which will label the plot with its corresponding percent.

Here is the donut part (or, rather, the donut hole). We are going to add a white circle to the center of the plot. In order for the aspect ratio to be equal, we must set this with matplotlib axis.

The entire code set is shown below:

And here is the result! It is a little messy, but it is a donut plot and it gives us some interesting insights into the common shapes of UFO sightings. Let’s spruce this up and add some ‘sprinkles’ to our donut plot.

Adding sprinkles to our donut plot

Obviously this chart is not going to win any awards (yet). If you are working with your own data, you might find that the above code is perfectly sufficient, with a few tweaks (colors, labels, etc). But fear not if you have found yourself with too many labels and not enough space on your donut plot.

For this application, I decided I really only cared about the most frequent shapes that people reported during their UFO sighting. I really wanted to see shapes that achieved >4% of the general consensus. The code below removes any shape that had less than 4% and adds their percentage to the “other” category. This will make the chart look a little neater. Feel free to tweak the code below as necessary for your application.

Alright, the plot is already looking better. Now, for the sprinkles…

From here, I recommend removing the labels and the autopct arguments from the pie chart function. It will look something like this:

Next, pop this into your favorite editing program (Photoshop, Illustrator, Keynote, PowerPoint). I use PowerPoint for all of my figure finalization (much to my designer husband’s disapproval). Add labels, titles, and icons.