# Matplotlib Subplots – Plot Multiple Graphs Using Matplotlib

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)
```

`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)
```

### 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

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')
```

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 `sharey` so that all the subplots axis have similar scale.

```#Import required libraries
import matplotlib.pyplot as plt

#Plotting
fig, ax = plt.subplots(2, 3, sharex=True, sharey=True)
for i in range(0,2):
for j in range(0,3):
ax[i][j].text(0.5, 0.5, str((i,j)),fontsize=18, ha='center')
```

`add_subplot` is an attribute of Matplotlib `figure` object. It is used whenever we wish to add subplots to our figure one by one.

Let’s demonstrate this with the example code.

```#Importing libraries
import matplotlib.pyplot as plt

df = data.data

#Create a figure object
fig = plt.figure(figsize=(8,8))

#Adding one subplot to the figure
ax_1 = fig.add_subplot(2, 2, 1) #selecting 1st out of 4 subplots
ax_1.scatter(df[:,0],df[:,1] , color = 'black')

ax_2.scatter(df[:,0],df[:,1] , color = 'red')
```

In the code above, the `add_subplot` attribute of the figure object requires a number of rows and columns as input argument along with the index of subplot.

But here instead of indexing subplots as 2D arrays, we simply need to pass an integer that resembles the figure number.

`fig.add_subplot(2, 2, 1)` in the code above will first create a 2×2 grid of subplots and return the 1st subplot axes object on which we can plot our data.

## Conclusion

In this article we saw how we can visualize data on multiple plots in a single figure, use of `subplots` method and number of ways to create subplots.

Happy Learning!