New Tutorials:   TKINTER    KOTLIN    JAVASCRIPT    SASS/SCSS    PL/SQL    Matplotlib    C++ Programs
See the Tutorial List

Matplotlib Pie Chart - pie() Function

In this tutorial, we will cover what is a Pie chart? and How to create pie charts to represent your data using the python matplotlib library.

What is a Pie chart?

A pie chart is basically a special chart that is used to show relative sizes of the data with the help of Pie slices. So, it is a complete circle to represent the 100% space and it creates pie slices to represent data sets.

  • It is a circular statistical plot that is used to display only one series of data.

  • The complete area of the pie chart is equal to the total percentage of the given data.

  • In the Pie Chart, the area of slices of the pie is used to represent the percentage of the parts of the data.

  • The slices of the Pie are commonly known as wedges.

  • The area of the wedge mainly represents the percentage of that part with respect to the whole data and can be calculated by the length of the arc of the wedge.

Uses of Pie charts:

Few uses are given below:

  1. Business Presentations(like in sales, survey results, and operations)

  2. Provides Quick Summary

Matplotlib pie() Function

The pie() function in the pyplot module of matplotlib is used to create a pie chart representing the data in an array.

The best pie chart can be created if the figure and axes are square, or the aspect of the Axes is equal.

The required syntax for the pie() function is given below:

matplotlib.pyplot.pie(data, explode, labels, colors, autopct, shadow)

pie() Function Parameters:

Let us discuss the parameters of this function:

1. data

This parameter is used to represents the array consisting of data values to be plotted, the fractional area of each slice is indicated by data/sum(data). If the sum(data)<1, then the data values return the fractional area directly, thus resulting pie will have an empty wedge of size = 1-sum(data).

2. labels

This parameter represents a list of the sequence of strings which is used to set the label of each wedge

3. autopct

This parameter is in the form of a string and is used to label the wedge with their numerical value

4. colors

This parameter is used to provide color to the wedges.

5. shadow

This parameter is used to create the shadow of the wedges.

Now, its time to brush up the concept and create some pie charts. So, let's start with some examples.

Simple Pie Chart Example:

In the given below example we are going to create a simple pie chart in the matplotlib:

import matplotlib.pyplot as plt
import numpy as np 

subjects = ['English', 'Hindi', 'Science', 'Arts', 'Music', 'Maths'] 

data = [23, 17, 35, 29, 12, 41] 
fig = plt.figure(figsize =(10, 7)) 
plt.pie(data, labels = subjects) 


plt.show() 

The output for the above code snippet is as shown below:

matplotlib simple pie chart example

Now it's time to create some customized pie charts. Here are some aspects for customizing the plots:

  • The startangle parameter is used to rotate the plot by the specified degrees in counter-clockwise direction performed on the x-axis of the pie chart.

  • The shadow parameter accepts a boolean value, if it is true then the shadow will appear below the rim of the pie.

  • To customize the Wedges of the pie the wedgeprop is used which takes Python dictionary as parameter with name values pairs that are used to denote the wedge properties like linewidth, edgecolor, etc.

  • By setting frame=True axes frame is drawn around the pie chart.

  • The autopct parameter mainly controls how to display the percentage on the wedges.

Custom Pie Chart Example:

Let us create a customized pie chart in the matplotlib:

import matplotlib.pyplot as plt

slices = [7,2,2,13]
activities = ['sleeping','cooking','working','singing']
cols = ['c','m','r','b']

plt.pie(slices,
        labels=activities,
        colors=cols,
        startangle=90,
        shadow= True,
        explode=(0,0.1,0,0),
        autopct='%1.1f%%')

plt.title('Customized Graph\n')
plt.show()

The output for the same is given below:

custom pie chart matplotlib example

Nested Pie Chart Example:

Now let us create a Nested Pie chart (also referred to as Donut Charts )and the live example for the same is given below: