Getting Started with Pie Charts

Getting Started with Pie Charts

·

8 min read

Pie charts. Common. They seem simple but are complex. Some people love them, and others loathe them. Whether you're a fan of pie charts or not, this tutorial aims to help you better understand pie charts. Here's what we'll learn:

  1. What are pie charts?

  2. Formatting tips and best practices

  3. Creating pie charts in Python using matplotlib

Let's get into it.

Pie Charts

Pie charts are circular-shaped charts divided into slices. The pie chart shows the relationship between a total value and the parts that make up this total value. The pie itself is the total value. The slices of the pie are the parts. Each slice is a percentage of the total. The chart should add up to 100%.

Pie charts come in two variations. The first is the regular pie chart as we know it. The second is the doughnut chart. The doughnut chart is a pie chart with a hole in the centre. It gets its name because its shape resembles that of a doughnut. A reference to pie charts in this tutorial includes doughnut charts.

The idea behind pie charts is simple. They display a parts-to-whole relationship. Three elements help achieve this:

  • The arc - the outer line on the slice that forms part of the circumference of the circle

  • The angle - calculated from the centre point of the chart moving outwards

  • The area - the size of the slice

These elements combine to form the visual.

Considering all three elements to determine the percentage for each slice is difficult. Having too many slices makes it more challenging. Misuse of the charts and bad formatting have given them a bad reputation.

Pie charts solely communicate a parts-to-whole relationship of the data. They are great when the goal is for the audience to get an idea of the proportions of each category of values. They are not ideal for comparing values to one another. When deciding whether to use a pie chart or not, ask yourself what it is that you want your audience to see. If the answer sways in the way of comparison and not parts-to-whole, then a pie chart is not appropriate.

Formatting Tips and Best Practices

When creating pie charts, there are some best practices to consider. These will make them more effective and readable. Let's discuss how to avoid the common pitfalls of pie charts.

Effects

Effects include:

  • adding shadows to the pie charts

  • exploding the charts

  • making the charts 3-D.

These sound like a good idea but they distort the charts. The effects may make the slices appear bigger or make the chart unreadable. Avoid adding effects as they may mislead the audience.

Use color wisely

Having many different colours on a pie chart causes visual clutter. This may be distracting. Be thoughtful and intentional when choosing colours. Prioritise colours that blend well together and create visual harmony. A popular way of achieving this is using shades of the same colour on the slices. You can add another colour to highlight or draw attention to a specific data point.

Sort the data

Humans tend to read anything on a circle in a clockwise direction. Take advantage of this and sort your data in descending order in the clockwise direction. This will make it easier to read the chart and discern largest to smallest. If you are visualising ordered data, it is advisable to preserve the order of the data and not sort it.

Title and Labels

The title and labels on your pie charts can be the difference between an informative pie chart and a vague one.

Choose a descriptive title for your chart to provide context. The title should answer the following questions:

  • What is the total value? You can go a step further and include the value in the title.

  • What are the categories that make up this total value?

The title should lay the groundwork for the pie chart to make sense. Make sure the audience understands the parts-to-whole relationship they are looking at.

Where possible, do not use a legend. Place your labels on the pie chart. The labels can go on the slices or next to the slices. This saves the reader a visual trip between the pie and a legend. Only use legends when you have information you can't fit on the pie chart.

Number of Slices

There are no rules on how many slices you can have on a pie chart, but the less you have, the better. A common suggestion is five slices or less. Limiting the number of slices makes the chart easier to read.

A great way to decrease the number of slices is to group smaller slices. You can label this grouped category 'Other' or choose a more descriptive label. Include a note or legend on what this other category includes. This will tidy up your chart and help with the labels. It takes a lot of work to label small slices.

Limiting the number of slices or grouping slices may not always be possible. In such cases, consider using a different chart. A stacked bar chart may be a better alternative. Stacked bar charts are capable of showing the parts-to-whole relationship. Read more about them and other bar charts here.

Pie charts are inherently difficult to read. They also carry some negative connotations. These may serve as the audience's barriers to understanding your chart. It is best to keep this in mind when creating them. Always keep things simple. Simplicity will be your pie chart's redemption.

Let's look at how we can use the above formatting tips to create pie charts.

Creating pie charts in Python using matplotlib

This section assumes you are familiar with Python, pandas and matplotlib. We will go through how to create the following:

  1. Pie chart

  2. Doughnut chart

We start by importing our modules. Next, we create a dictionary named units to convert into a dataframe.

import matplotlib.pyplot as plt
import pandas as pd

units = {
    'Items': ['Sweaters', 'T-Shirts', 'Jeans', 'Sneakers'],
    'Q4': [19, 69, 41, 28]
}

df = pd.DataFrame(units)

Running the code creates a simple dataframe like the table below. This data shows the number of units sold in the fourth and last quarter of 2022.

ItemsQ4
Sweaters19
T-Shirts69
Jeans41
Sneakers28

Great! We have our data. Before getting into the plotting, let's look at the data. The items are unordered. We can sort the dataframe by the values in the Q4 column in descending order. This will allow us to create an easily readable chart.

df_sorted = df.sort_values(by=['Q4'], ascending=False)

Lastly, let's choose colours for our charts and store them in a list. This allows us to pass the list as an argument when we call the functions to create our charts.

chart_colors = ['#29AB87', '#676767', '#8B8C89', '#808080']

Let's get started now that we have what we need. We will create the pie chart first, then tackle the doughnut chart afterwards.

Pie Chart

Plotting a pie chart in matplotlib is rather straightforward. We call the plt.pie() function with the following arguments:

  1. The data we are visualising.

  2. labels - The labels for the different items in our dataset

  3. autopct - This allows us to specify the formats of the percentages on our chart.

  4. colors - allows us to specify the colours for our different slices.

  5. startangle - allow us to specify the starting angle on our chart. The default starting angle is zero.

  6. counterclock - specifies the direction of the slices on the chart. Counterclockwise is the default.

plt.pie(df_sorted['Q4'], labels=df_sorted['Items'], autopct='%1.1f%%', colors=chart_colors, startangle=90, counterclock=False)
plt.title("\nNumber of Units Sold per Item in Q4 (157)\n")

We added a descriptive title using plt.title(). Our code will give us the chart below.

And that's it for our pie chart. Next, we create our doughnut chart.

Doughnut Chart

Matplotlib does not have a dedicated function for plotting doughnut charts. We will first plot a pie chart using plt. pie(). Then, we'll create the centre hole for the doughnut by drawing and placing a circle on the pie chart. Let's get started.

plt.pie(df_sorted['Q4'], labels=df_sorted['Items'], autopct='%1.1f%%', colors=chart_colors, startangle=90, counterclock=False)
plt.title("\nPercentages of Units Sold per Item in Q4 (157)\n")
centre_circle = plt.Circle((0,0),0.4, fc='white')
fig = plt.gcf()
fig.gca().add_artist(centre_circle)

The first line of code plots our pie chart. The second line gives it a title. In the third line, we use plt. Circle with the following arguments:

  • the x and y coordinates for the centre of the circle

  • the size of the circle

  • the fill colour.

We store the circle in a variable named centre_circle. We will use it to create the centre hole for the doughnut. Next, we use plt.gcf(). This is a command to get the current figure, our pie chart. We store the current figure as a variable named fig. Now, we need to get access to the chart's axis. This will allow us to add the circle to the centre. We use plt.gca() to give us access to the axis and add_artist to add the centre circle to the chart. Our code produces the chart below.

And that's a wrap. We have learned how to create and style a pie chart in matplotlib. We have also hacked our way into creating a doughnut chart.

Where to Go From Here

This tutorial is an introduction to creating pie charts using Python and matplotlib. We covered just the basics. If you would like to learn more, I would recommend the following resources:

  • Matplotlib documentation which can be found here

  • storytelling with data by Cole Nassbaumer Knaflic is a great book to have for anyone working with data. You can probably find it wherever you buy your books.

Conclusion

In this tutorial, we:

  • learned about pie charts and their best use cases

  • explored some formatting tips and best practices

  • created a pie chart and a doughnut chart using matplotlib

I hope you found this tutorial helpful and informative. Until next time, happy visualising!