Matplotlib Subplots – Plot Multiple Graphs Using Matplotlib

In this article, we will learn how to create Matplotlib subplots.

In practice we often need more than one plot to visualize the variables, this is when subplots come into the picture. Matplotlib subplot method is a convenience function provided to create more than one plot in a single figure.

Creating a Basic Plot Using Matplotlib

To create a plot in Matplotlib is a simple task, and can be achieved with a single line of code along with some input parameters. The code below shows how to do simple plotting with a single figure.

#Importing required libraries 
import matplotlib.pyplot as plt
import numpy as np

#Create data
data = np.arange(1,5,1)

#Plotting the data:
plt.plot(data)
Simple Plotting
Simple Plotting

plt.plot() displays the line plot of input data.

Creating Matplotlib Subplots

Now think of a situation where we need to have multiple plots for explaining our data. For example, we have a dataset having temperature and rainfall rate as variables and we need to visualize the data.

One thing that occurs to mind is to plot both variables in a single plot, but the measurement scale for temperature (Kelvin) is different than that of rainfall rate(mm).

Here we need a separate plot for both in order to have visual interpretation. Matplotlib subplot is what we need to make multiple plots and we’re going to explore this in detail.

1. Using the subplots() method

Let’s have some perspective on using matplotlib.subplots.

The matplotlib subplots() method requires a number of rows and a number of columns as an input argument to it and it returns a figure object and axes object.

Each axis object can be accessed using simple indexing. And after having selected the required axes to plot on, the procedure for plotting will follow its normal course as we did in the above code.

Let’s create 4 subplots arranged like a grid.

#Importing required libraries
import matplotlib.pyplot as plt

# Creates fig and ax from subplots().
fig , ax = plt.subplots(nrows = 2, ncols = 2)
4 Subplots
4 Subplots

2. Accessing subplots

Accessing individual axes is very simple. Let’s do some plotting on first and the last subplot.

import matplotlib.pyplot as plt
from sklearn.datasets import load_iris

#Loading Dataset
data = load_iris()
df = data.data

fig , ax = plt.subplots(nrows = 2, ncols = 2, figsize=(8,6))

#Plotting on the 1st axes
ax[0][0].scatter(df[:,0],df[:,1] , color = 'black')

#Plotting on the last axes
ax[1][1].scatter(df[:,1],df[:,2] , color = 'red')
Accessing 1st And 4th Subplots
Accessing 1st And 4th Subplots

Think of each axes as some objects arranged in an 2D array, accessing each subplot is similar to accessing elements from 2D array.

  • ax[0][0] means we first selected first row (index 0) and the first element from that row (index 0).
  • ax[1][1] means we first selected the second row (index 1) and the second element from that row (index 1).

3. Matplotlib Subplots with shared axis

In many applications, we need the axis of subplots to be aligned with each other. The matplotlib subplots() method accepts two more arguments namely sharex and