0
0
Matplotlibdata~5 mins

Why multiple plots per figure matter in Matplotlib

Choose your learning style9 modes available
Introduction

Showing multiple plots in one figure helps compare data easily. It saves space and makes patterns clearer.

Comparing sales data of different products side by side.
Showing temperature changes in different cities on the same day.
Displaying different parts of a dataset to find relationships.
Presenting before and after results of an experiment together.
Syntax
Matplotlib
import matplotlib.pyplot as plt

fig, axs = plt.subplots(nrows, ncols)

axs[row, col].plot(data_x, data_y)
plt.show()

plt.subplots() creates a grid of plots inside one figure.

You can control rows and columns to organize your plots.

Examples
Two plots side by side in one figure.
Matplotlib
fig, axs = plt.subplots(1, 2)
axs[0].plot([1, 2, 3], [4, 5, 6])
axs[1].plot([1, 2, 3], [6, 5, 4])
plt.show()
Four plots arranged in 2 rows and 2 columns.
Matplotlib
fig, axs = plt.subplots(2, 2)
axs[0, 0].plot([1, 2], [3, 4])
axs[0, 1].plot([1, 2], [4, 3])
axs[1, 0].plot([1, 2], [2, 5])
axs[1, 1].plot([1, 2], [5, 2])
plt.show()
Sample Program

This program shows two different plots stacked vertically in one figure. It helps compare line and bar charts easily.

Matplotlib
import matplotlib.pyplot as plt

# Create 2 rows and 1 column of plots
fig, axs = plt.subplots(2, 1)

# First plot: simple line
axs[0].plot([0, 1, 2], [0, 1, 4])
axs[0].set_title('Line Plot')

# Second plot: simple bar
axs[1].bar(['A', 'B', 'C'], [3, 7, 5])
axs[1].set_title('Bar Plot')

plt.tight_layout()
plt.show()
OutputSuccess
Important Notes

Use plt.tight_layout() to avoid overlapping labels.

Each subplot can have its own title and labels.

Multiple plots in one figure make your data story clearer and save space.

Summary

Multiple plots per figure help compare data side by side.

Use plt.subplots() to create grids of plots.

Each plot can be customized independently inside the figure.