Create a pie chart using Matplotlib in Python

This article will clear all your doubts related to creating a pie chart using the Matplotlib library in Python. All the methods, from creating a basic pie chart to a customized pie chart have been discussed in this tutorial. So, read the text below very carefully to learn new methods and enjoy programming.

Before continuing, kindly ensure your code space has the Matplotlib library installed. If not, you may install it using the following syntax.

pip install matplotlib

Basic pie chart

A basic pie chart can be programmed using the following code in Python.

# import the matplotlib library
from matplotlib import pyplot as plt
# define the labels of your chart
Subjects=['Physics','Chemistry','Maths','English','Social Science']
# data of the chart
Marks=[93,70,80,84,70]
# plot the pie chart 
plt.pie(Marks,labels=Subjects)
# to view the chart
plt.show()
Output:

This is a very basic pie chart, but you can modify this pie chart using several methods in your program.

Colors:

With the help of this method, you can change the colors of all the slices of the pie chart according to your preference. An example is shown below.

# import the matplotlib library
from matplotlib import pyplot as plt
# define the labels of your chart
Subjects=['Physics','Chemistry','Maths','English','Social Science']
# data of the chart
Marks=[93,70,80,84,70]
# color of your preference
color=['White','Black','Yellow','Indigo','Magenta']
# plot the pie chart 
plt.pie(Marks,labels=Subjects,colors=color)
# to view the chart
plt.show()
Output:

Wedge:

To make the pie chart look more glaring to the eyes, you can color the edge of the pie chart and aslo modify the width of the edges using the method wedgeprops mentioned below.

# import the matplotlib library
from matplotlib import pyplot as plt
# define the labels of your chart
Subjects=['Physics','Chemistry','Maths','English','Social Science']
# data of the chart
Marks=[93,70,80,84,70]
# color of your preference
color=['Grey','Black','Yellow','Indigo','Magenta']
# wedge of the pie chart
wedge={'edgecolor':'blue','linewidth':2}
# plot the pie chart 
plt.pie(Marks,labels=Subjects,colors=color,wedgeprops=wedge)
# to view the chart
plt.show()
Output:

Explode:

You can use this method to explode any or all the slices from the pie chart, i.e… to pull a slice from the pie chart as shown below.

# import the matplotlib library
from matplotlib import pyplot as plt 
# define the labels of your chart
Subjects=['Physics','Chemistry','Maths','English','Social Science']
# explode physics and maths
exp=[0.2,0,0.4,0,0]
# data of the chart
Marks=[93,70,80,84,70]
# color of your preference
color=['Grey','Black','Yellow','Indigo','Magenta']
# wedge of the pie chart
wedge={'edgecolor':'blue','linewidth':2}
# plot the pie chart
plt.pie(Marks,labels=Subjects,colors=color,wedgeprops=wedge,explode=exp)
# to view the chart 
plt.show()
Output:

Shadow:

You can show the shadow of all the slices of the pie chart by setting shadow equal to True.

# import the matplotlib library
from matplotlib import pyplot as plt 
# define the labels of your chart
Subjects=['Physics','Chemistry','Maths','English','Social Science']
# explode physics and maths
exp=[0.2,0,0.4,0,0]
# data of the chart
Marks=[93,70,80,84,70]
# color of your preference
color=['Grey','Black','Yellow','Indigo','Magenta']
# wedge of the pie chart
wedge={'edgecolor':'blue','linewidth':2}
# plot the pie chart
plt.pie(Marks,labels=Subjects,colors=color,wedgeprops=wedge,explode=exp,shadow=True)
# to view the chart 
plt.show()
Output:

Legend:

This method is used to show which color depicts which label of the pie chart. An example is shown below.

# import the matplotlib library
from matplotlib import pyplot as plt 
# define the labels of your chart
Subjects=['Physics','Chemistry','Maths','English','Social Science']
# explode physics and maths
exp=[0.2,0,0.4,0,0]
# data of the chart
Marks=[93,70,80,84,70]
# color of your preference
color=['Grey','Black','Yellow','Indigo','Magenta']
# wedge of the pie chart
wedge={'edgecolor':'blue','linewidth':2}
# plot the pie chart
plt.pie(Marks,labels=Subjects,colors=color,wedgeprops=wedge,explode=exp,shadow=True)
# plot legend
plt.legend(title='subjects',bbox_to_anchor=(0.5,-0.1))
# to view the chart 
plt.show()
Output:

 

Leave a Reply

Your email address will not be published. Required fields are marked *