Create Subplots in Matplotlib featured image

How to Create Subplots in Matplotlib ? Only 4 Steps

Matplotlib is a great python package for the visualization of your data. In this entire tutorial, you will learn how to create subplots in Matplotlib through step by step guide.

 

Steps by Steps to Create Subplots in Matplotlib

Step 1: Learn the Syntax to create matplotlib subplot.

The method for the matplotlib subplot is pyplot.subplot(). There are some arguments you have to pass to create subplots.

matplotlib.pyplot.subplots ( nrows =1,ncols =1 ,sharex = False,sharey= True)

Parameters  Description

nrows : It denotes the number of rows of the subplot. The default value is 1.

ncols:  Default value is 1. It denotes the number of columns for the plot.

sharex : It allows you to share the x-axis for all the subplots. False is the default value.

sharey : It allows you to share the y-axis value for the subplots. The default value is False.

You will understand it more in further examples.

Step 2: Import all the necessary libraries.

The first and most important step is to import all the necessary python libraries. Here in this tutorial, I will use only NumPy with matplotlib.

import numpy as np
import matplotlib.pyplot as plt

Step 3: Create data to create subplots in matplotlib.

Now let’s create different data points that I will use to Create Subplots in Matplotlib. Here I am creating four data points.

#data 1
x1 = [10,20,30,40,50]
y1 = [1,2,3,4,5]
# data2
x2 = np.array([1,2,3,4,5])
y2 = np.cos(x2**2)
#data3
x3 = np.linspace(0, 2 * np.pi, 400)
y3 = np.sin(x3 ** 2)
#data4
x4 = np.linspace(0, 2 * np.pi, 400)
y4 = np.log(x4 ** 2)

Step 4: Create Subplots in Matplotlib

The data points are already created above use them here.

Single Subplot

# data1
x1 = [10,20,30,40,50]
y1 = [1,2,3,4,5]

fig, ax = plt.subplots()
ax.plot(x1, y1)
ax.set_title('Single plot')
plt.show()

You can see nothing is passed as arguments inside the method subplots(). It means one row and one column or one plot will be in the figure. The method will return fig and ax. When you will run the above program you will get the same output.

Single Matplotlib Subplot
Single Matplotlib Subplot

Create two Subplots in Matplotlib

To create two matplotlib subplots you have to pass the 1 and 2  or 2 and 1 as arguments for no of rows and no of columns. If you will use 1 and 2 then the plots will be in arranged column-wise and if you use 2 and 1 the rowwise.

# data 1
x1 = [10,20,30,40,50]
y1 = [1,2,3,4,5]

# data2
x2 = np.array([1,2,3,4,5])
y2 = np.cos(x2**2)

fig, (ax1,ax2) = plt.subplots(1,2)

ax1.plot(x1, y1)
ax2.plot(x2, y2)
ax1.set_title('First plot')
ax2.set_title('Second plot')
plt.show()

 

Here ax1 and ax2 are will get the value of the axis array returned by the subplots() method.

Two Matplotlib Subplots
Two Matplotlib Subplots

The above was the example of a single subplot. Let’s create multiple subplots of four data points. The method will be the same as the above but the arguments differ.

Create Four Subplots in Matplotlib

Just like you have created two plots by passing 1 and 2 as arguments to the subplots() method. You have to pass 2 and 2 to plot the four matplotlib subplots.

# data 1
x1 = [10,20,30,40,50]
y1 = [1,2,3,4,5]

# data2
x2 = np.array([1,2,3,4,5])
y2 = np.cos(x2**2)

# data3
x3 = np.linspace(0, 2 * np.pi, 400)
y3 = np.sin(x3 ** 2)

# data4
x4 = np.linspace(0, 2 * np.pi, 400)
y4 = np.log(x4 ** 2)

fig, ((ax1,ax2),(ax3,ax4)) = plt.subplots(2,2)

ax1.plot(x1, y1)
ax2.plot(x2, y2)
ax3.plot(x3, y3)
ax4.plot(x4, y4)
ax1.set_title('First plot')
ax2.set_title('Second plot')
ax3.set_title('Third plot')
ax3.set_title('Fourth plot')
plt.show()
Four Matplotlib Subplots
Four Matplotlib Subplots

You can see in the above code We are getting the axis array value to each variable ax1,ax2,ax3, and ax4. It allows you to plot each individual figure in your own axis.

You can read more about it on official creating subplots in matplotlib documentation.

Join our list

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.

Thank you for signup. A Confirmation Email has been sent to your Email Address.

Something went wrong.

 
Thank you For sharing.We appreciate your support. Don't Forget to LIKE and FOLLOW our SITE to keep UPDATED with Data Science Learner