Multiplots in Python using Matplotlib
Last Updated :
28 Apr, 2025
Matplotlib is a Python library that can be used for plotting graphs and figures. Plotting multiplots or multiple plots are often required either for comparing the two curves or show some gradual changes in the multiple plots, and this can be done using Subplots. Subplots are one of the most important and fundamental concepts to be understood while trying to plot multiple figures/graphs/plots in the same, and this tutorial shows you how it is done. The subplot() function can be found in the pyplot module of matplotlib, which provides a MATLAB-like framework for plotting in Python. The function's prototype is as shown below:
matplotlib.pyplot.subplot(nrows, ncols, index)
There are also certain keyword arguments that can be given.
The Stacked Plots
First, let's try to plot a sequence of plots (3 here) by stacking one over the other. As we are stacking them, it's the number of rows (nrows) that change, meaning ncols stay the same as 1. Each subplot is identified by the index parameter.
Python3
import matplotlib.pyplot as plt
import numpy as np
x = np.arange(0, 10, 0.01)
# notice that nrows = number
# of images
plt.subplot(3, 1, 1)
plt.plot(x, np.sin(x))
# ncols stays as 1
plt.subplot(3, 1, 2)
plt.plot(x, np.cos(x))
# each image has a unique
# index
plt.subplot(3, 1, 3)
plt.plot(x, x)
plt.show()
Output: 
Grid Plots
Next, we'll try to plot some graphs as a grid, that is, multiplots with multiple columns. Here, we will create a multiplot with 4 plots, we will make it a 2x2 grid, which means nrows=2 and ncols=2, with each image being identified by the index again, in a row-major order.
Python3
import matplotlib.pyplot as plt
import numpy as np
x = np.arange(0, 10, 0.01)
plt.subplot(2, 2, 1)
plt.plot(x, np.sin(x))
plt.subplot(2, 2, 2)
plt.plot(x, np.cos(x))
plt.subplot(2, 2, 3)
plt.plot(x, x)
plt.subplot(2, 2, 4)
plt.plot(x, 5-x)
plt.show()
Output:
If we were to create a multiplot where one plot overlaps over the other one, kind of like what we see when we minimize a youtube video but it still runs in a small window at the bottom corner, then the add_suplplot() function helps in this.
Python3
import matplotlib.pyplot as plt
import numpy as np
fig = plt.figure()
x = np.arange(0, 10, 0.01)
plt.plot(x, x-5)
plt.title("Not so Tiny")
fig.add_subplot(2, 2, 4)
plt.plot(x, 5-x)
plt.title("Tiny Plot")
plt.show()
Output:
Oh, and also, for all the subplot functions that we've implemented, if nrows, ncols, and index are less than 10, instead of giving the arguments in the (nrows, ncols, index) format, they can be passed as a three-digit in the format (nrows*100+ncols*10+index), and the output would remain the same. Why don't we try it out with the Grid Plots?
Python3
import matplotlib.pyplot as plt
import numpy as np
x = np.arange(0, 10, 0.01)
plt.subplot(221)
plt.plot(x, np.sin(x))
plt.subplot(222)
plt.plot(x, np.cos(x))
plt.subplot(223)
plt.plot(x, x)
plt.subplot(224)
plt.plot(x, 5-x)
plt.show()
Output:
The output still remains the same even if we use the three-digit format of passing the arguments.
Similar Reads
Simple Plot in Python using Matplotlib Creating simple plots is a common step in data visualization. These visual representations help us to understand trends, patterns and relationships within data. Matplotlib is one of the most popular plotting libraries in Python which makes it easy to generate high-quality graphs with just a few line
4 min read
Matplotlib.pyplot.sci() in Python Matplotlib is a library in Python and it is numerical - mathematical extension for NumPy library. Pyplot is a state-based interface to a Matplotlib module which provides a MATLAB-like interface. There are various plots which can be used in Pyplot are Line Plot, Contour, Histogram, Scatter, 3D Plot,
2 min read
Contour Plot using Matplotlib - Python Contour plots also called level plots are a tool for doing multivariate analysis and visualizing 3-D plots in 2-D space. If we consider X and Y as our variables we want to plot then the response Z will be plotted as slices on the X-Y plane due to which contours are sometimes referred as Z-slices or
2 min read
How to Plot Mfcc in Python Using Matplotlib? Mel-frequency cepstral coefficients (MFCC) are widely used in audio signal processing and speech recognition tasks. They represent the spectral characteristics of an audio signal and are commonly used as features for various machine-learning applications. In this article, we will explore how to comp
2 min read
Matplotlib.pyplot.twiny() in Python Matplotlib is a library in Python and it is numerical - mathematical extension for NumPy library. Pyplot is a state-based interface to a Matplotlib module which provides a MATLAB-like interface. Sample Code Python3 1== # sample code import matplotlib.pyplot as plt plt.plot([1, 2, 3, 4], [16, 4, 1, 8
2 min read
Matplotlib.pyplot.twinx() in Python Matplotlib is a library in Python and it is numerical - mathematical extension for NumPy library. Pyplot is a state-based interface to a Matplotlib module which provides a MATLAB-like interface. Sample Code Python3 1== # sample code import matplotlib.pyplot as plt plt.plot([1, 2, 3, 4], [16, 4, 1, 8
2 min read
Matplotlib.pyplot.sca() in Python Matplotlib is a library in Python and it is numerical â mathematical extension for NumPy library. Pyplot is a state-based interface to a Matplotlib module which provides a MATLAB-like interface. There are various plots which can be used in Pyplot are Line Plot, Contour, Histogram, Scatter, 3D Plot,
1 min read
Matplotlib.pyplot.quiver() in Python Matplotlib is a library of Python bindings which provides the user with a MATLAB-like plotting framework. Matplotlib can be used in Python scripts, the Python and IPython shell, web application servers, and various graphical user interface toolkits like Tkinter, awxPython, etc. Matplotlib.pyplot.qui
2 min read
Matplotlib.pyplot.xlim() in Python Matplotlib is a library in Python and it is numerical - mathematical extension for NumPy library. Pyplot is a state-based interface to a Matplotlib module which provides a MATLAB-like interface. There are various plots which can be used in Pyplot are Line Plot, Contour, Histogram, Scatter, 3D Plot,
2 min read
Matplotlib.pyplot.show() in Python Matplotlib is a library in Python and it is numerical - mathematical extension for NumPy library. Pyplot is a state-based interface to a Matplotlib module which provides a MATLAB-like interface. Sample Code - Python3 1== # sample code import matplotlib.pyplot as plt plt.plot([1, 2, 3, 4], [16, 4, 1,
2 min read