Plot multiple plots in Matplotlib
Matplotlib allows you to display multiple graphs in a single figure, making it easier to analyze trends, compare data sets and understand relationships between variables. In this article, we’ll explore how to plot multiple graphs in one figure using Matplotlib, helping you create clear and organized visualizations. Below are the different methods to plot multiple plots in Matplotlib.
Using subplot()
subplot() function lets you divide a figure into a grid and place multiple plots in different sections. It’s useful when you want to compare several graphs side by side in a single figure.
Syntax
plt.subplots(nrows=1, ncols=1, sharex=False, sharey=False)
Parameters:
- nrows, ncols: Number of rows and columns of subplots (default=1).
- sharex, sharey: Whether subplots share x or y axes (default=False).
Example:
import matplotlib.pyplot as plt
import numpy as np
import math
x = np.arange(0, 2*math.pi, 0.05)
y1, y2, y3, y4 = np.sin(x), np.cos(x), np.tan(x), np.tanh(x)
fig, ax = plt.subplots(2, 2)
ax[0,0].plot(x, y1); ax[0,0].set_title("Sine")
ax[0,1].plot(x, y2); ax[0,1].set_title("Cosine")
ax[1,0].plot(x, y3); ax[1,0].set_title("Tangent")
ax[1,1].plot(x, y4); ax[1,1].set_title("Tanh")
plt.show()
Output

Explanation:
- plt.subplots(2, 2): Creates a 2×2 grid of subplots.
- ax[i,j].plot(x, y): Plots a curve in the subplot at row i, column j.
- y1, y2, y3, y4 = np.sin(x), np.cos(x), np.tan(x), np.tanh(x): Computes the values for the four different functions.
Using subplot2grid()
subplot2grid() allows you to place and size plots freely within a grid. You can make a plot span multiple rows or columns, giving more control than subplot().
Syntax
plt.subplot2grid(shape, loc, rowspan=1, colspan=1)
Parameters:
- shape: Grid size (rows, cols).
- loc: Starting position (row, col).
- rowspan, colspan: Number of rows/columns the plot spans.
Example:
import matplotlib.pyplot as plt
import numpy as np
a = plt.subplot2grid((3,3), (0,0), colspan=2)
b = plt.subplot2grid((3,3), (0,2), rowspan=3, colspan=1)
c = plt.subplot2grid((3,3), (1,0), rowspan=2)
x = np.arange(1, 10)
a.plot(x, np.exp(x)); a.set_title("Exponent")
b.plot(x, x**0.5); b.set_title("Square Root")
c.plot(x, x**2); c.set_title("Square")
plt.tight_layout()
plt.show()
Output

Explanation:
- plt.subplot2grid((3,3), (row, col), rowspan=?, colspan=?): Places and sizes subplots in a grid.
- plot(x, y): Draws the curve in the specified subplot.
- plt.tight_layout(): Prevents overlapping of titles and labels.
Multiple Curves in a Single Plot
A multiple curves plot is a graph where more than one function or data series is drawn on the same set of axes. This allows you to compare the curves directly and see how they relate to each other in terms of trends, peaks or intersections, all within a single figure.
Example:
import matplotlib.pyplot as plt
import numpy as np
import math
x = np.arange(0, 2*math.pi, 0.05)
sin_y, cos_y = np.sin(x), np.cos(x)
plt.plot(x, sin_y, color='r', label='sin')
plt.plot(x, cos_y, color='g', label='cos')
plt.xlabel("Angle")
plt.ylabel("Magnitude")
plt.title("Sine & Cosine Functions")
plt.legend()
plt.show()
Output

Explanation:
- np.arange(0, 2*math.pi, 0.05): Creates an array of values from 0 to 2π with a step of 0.05 for plotting.
- plt.plot(x, y, color=…, label=…): Plots a curve and assigns a color and label.