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:
Business Presentations(like in sales, survey results, and operations)
Provides Quick Summary
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:
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).
This parameter represents a list of the sequence of strings which is used to set the label of each wedge
This parameter is in the form of a string and is used to label the wedge with their numerical value
This parameter is used to provide color to the wedges.
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:
Now it's time to create some customized pie charts. Here are some aspects for customizing the plots:
startangleparameter is used to rotate the plot by the specified degrees in counter-clockwise direction performed on the x-axis of the pie chart.
shadowparameter 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
wedgepropis used which takes Python dictionary as parameter with name values pairs that are used to denote the wedge properties like linewidth, edgecolor, etc.
frame=Trueaxes frame is drawn around the pie chart.
autopctparameter 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:
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: