Pie Charts with Matplotlib

Learn to build informative pie charts in Python using Matplotlib.

Pie Charts with Matplotlib
Pie charts in Matplotlib

Topics Covered

  1. What is a Pie chart?
  2. Syntax of pie chart function
  3. Plot a pie chart in Matplotlib
  4. Customizing a pie chart in Python

What is a Pie chart?

A pie chart is a circular statistical graphic, which is divided into slices to illustrate numerical proportions. In a pie chart, the arc length of each slice is proportional to the quantity it represents.

A pie chart consists of a circle with each pie representing a specific category. Unlike bar and line charts, pie charts donot show changes over time.


Syntax of pie chart function


The syntax of pie() function is given below.

matplotlib.pyplot.pie(data, explode=None, labels=None, colors=None, autopct=None, shadow=False)
  • data - The array of data values to be plotted
  • labels - It is a list of sequence of strings which sets the label of each slice.
  • color - Adds color to the slices.
  • autopct - It is used to label the slices with their numerical value.
  • shadow - It is used to create the shadow for each slice

Plot a pie chart in Matplotlib

Let's try to plot the sales of different products sold by a retailer for a particular month.

Total sales by a retailer in March 2023

Let's plot a basic pie chart with Matplotlib to show the percentage of sales per product.

import matplotlib.pyplot as plt

labels = ['Product A', 'Product B', 'Product C', 'Product D', 'Product E']
sizes = [23, 28, 42, 18, 36]
 
fig1, ax1 = plt.subplots()
ax1.pie(sizes, labels=labels)
ax1.axis('equal')  
plt.show()

The above code generates the following figure.

Simple pie chart in Matplotlib

By default the plotting of the first slice starts from the x-axis and moves counterclockwise.


Customizing a pie chart in Python

Rotating the pie chart

As mentioned the default start angle of a slice is at the x-axis. You can change the start angle by specifying a startangle parameter. It is defined with an angle in degrees, default angle is 0.

import matplotlib.pyplot as plt

labels = ['Product A', 'Product B', 'Product C', 'Product D', 'Product E']
sizes = [23, 28, 42, 18, 36]
 
fig1, ax1 = plt.subplots()
ax1.pie(sizes, labels=labels, startangle = 30)
ax1.axis('equal')  
plt.show()
Rotating a pie chart in Matplotlib

Making a slice pop out

Maybe you want one of the slices to pop out? The explode parameter allows you to pop one or more slices as per your requirement. The explode parameter must be an array with one value for each slice. Each value represents how far from the center each slice is displayed.

Let's try to explode the 2nd slice.

import matplotlib.pyplot as plt

labels = ['Product A', 'Product B', 'Product C', 'Product D', 'Product E']
sizes = [23, 28, 42, 18, 36]
explode = (0, 0.2, 0, 0, 0) 

fig1, ax1 = plt.subplots()
ax1.pie(sizes, explode=explode, labels=labels, startangle = 30)
ax1.axis('equal')  
plt.show()
Explode a pie chart slice in Matplotlib

To pop out all slices, add an explode value for each of the slice.

import matplotlib.pyplot as plt

labels = ['Product A', 'Product B', 'Product C', 'Product D', 'Product E']
sizes = [23, 28, 42, 18, 36]
explode = (0.1, 0.2, 0.3, 0.4, 0.5) 

fig1, ax1 = plt.subplots()
ax1.pie(sizes, explode, labels=labels, startangle = 30)
ax1.axis('equal')  
plt.show()
Exploding each slice of a pie chart in Matplotlib

Displaying the percentage for each slice

To display the percentage of each slice, use the autopct parameter in your code.

import matplotlib.pyplot as plt

labels = ['Product A', 'Product B', 'Product C', 'Product D', 'Product E']
sizes = [23, 28, 42, 18, 36]


fig1, ax1 = plt.subplots()
ax1.pie(sizes, labels=labels, startangle = 30, autopct='%1.1f%%')
ax1.axis('equal')  
plt.show()
Displaying the percentage of each slice in a pie chart in Matplotlib

Adding a shadow

You can add a shadow to the pie chart by setting the shadows parameter to True.

import matplotlib.pyplot as plt

labels = ['Product A', 'Product B', 'Product C', 'Product D', 'Product E']
sizes = [23, 28, 42, 18, 36]
explode = (0, 0.2, 0, 0, 0) 

fig1, ax1 = plt.subplots()
ax1.pie(sizes, shadow=True, explode=explode, labels=labels, startangle = 30)
ax1.axis('equal')  
plt.show()
Adding shadow to a pie chart in Matplotlib

Changing colors of the slices

You can choose to define the color of each wedge using the colors parameter. It must be an array with one value for each slice.

import matplotlib.pyplot as plt

labels = ['Product A', 'Product B', 'Product C', 'Product D', 'Product E']
sizes = [23, 28, 42, 18, 36]
explode = (0, 0.2, 0, 0, 0) 
colors = ["black", "red", "blue", "#CB7B97", "m"]

fig1, ax1 = plt.subplots()
ax1.pie(sizes, colors=colors, shadow=True, explode=explode, labels=labels, startangle = 30)
ax1.axis('equal')  
plt.show()
Defining your own colors for the pie chart slices in Matplotlib

You can use any Hexadecimal color values, any of the 140 supported color names, or one of these shortcuts:

'r' - Red
'g' - Green
'b' - Blue
'c' - Cyan
'm' - Magenta
'y' - Yellow
'k' - Black
'w' - White

Adding legend to your pie chart

Use the legend() function to add necessary legends for each of the slices.

import matplotlib.pyplot as plt

labels = ['Product A', 'Product B', 'Product C', 'Product D', 'Product E']
sizes = [23, 28, 42, 18, 36]
explode = (0, 0.2, 0, 0, 0) 

fig1, ax1 = plt.subplots()
ax1.pie(sizes, shadow=True, explode=explode, labels=labels, startangle = 30)
ax1.axis('equal')  
plt.legend()
plt.show()
Adding legend to the pie chart in Matplotlib

If you want to add a header to the legend, use the title parameter with the legend function.

import matplotlib.pyplot as plt

labels = ['Product A', 'Product B', 'Product C', 'Product D', 'Product E']
sizes = [23, 28, 42, 18, 36]
explode = (0, 0.2, 0, 0, 0) 

fig1, ax1 = plt.subplots()
ax1.pie(sizes, shadow=True, explode=explode, labels=labels, startangle = 30)
ax1.axis('equal')  
plt.legend(title = 'Sales percentage')
plt.show()
Adding a title to the legend of a pie chart in Matplotlib

Pylenin has a dedicated Youtube playlist for Matplotlib Tutorial. Check out our entire Matplotlib playlist here.

Subscribe to Pylenin

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
jamie@example.com
Subscribe