So far, we have been creating one plot per figure. But in real-world data analysis, it’s often useful to display multiple plots together for comparison. This is where subplots come in.
Matplotlib allows you to create multiple plots (subplots) inside a single figure, arranged in rows and columns.
📌 What are Subplots?
- A subplot is simply one plot within a figure that can contain multiple plots.
- Using subplots, you can easily compare different datasets or different types of charts side by side.
- The function
plt.subplot()
or the more flexibleplt.subplots()
is used to create them.
📊 Example 1: Using plt.subplot()
The plt.subplot(nrows, ncols, index)
function divides the figure into a grid of subplots.
import matplotlib.pyplot as plt
# Data
x = [1, 2, 3, 4, 5]
y1 = [1, 4, 9, 16, 25]
y2 = [25, 16, 9, 4, 1]
# Create subplots
plt.subplot(1, 2, 1) # 1 row, 2 columns, 1st plot
plt.plot(x, y1, 'b-o')
plt.title("y = x^2")
plt.subplot(1, 2, 2) # 1 row, 2 columns, 2nd plot
plt.plot(x, y2, 'r-s')
plt.title("y = reverse(x^2)")
plt.suptitle("Subplot Example with plt.subplot()")
plt.show()
✅ This will create two plots in one row for comparison.
📊 Example 2: Using plt.subplots()
(Recommended)
plt.subplots()
is more powerful and flexible. It returns a figure object and an array of axes objects.
# Create 2 rows and 2 columns of subplots
fig, axs = plt.subplots(2, 2)
# First plot
axs[0, 0].plot(x, y1, 'b')
axs[0, 0].set_title("y = x^2")
# Second plot
axs[0, 1].plot(x, y2, 'r')
axs[0, 1].set_title("y = reverse(x^2)")
# Third plot
axs[1, 0].bar(x, y1, color='green')
axs[1, 0].set_title("Bar Chart")
# Fourth plot
axs[1, 1].scatter(x, y2, color='purple')
axs[1, 1].set_title("Scatter Plot")
# Adjust layout
plt.suptitle("Subplot Example with plt.subplots()")
plt.tight_layout()
plt.show()
✅ This creates a 2×2 grid of subplots with line, bar, and scatter charts.
🎨 Customizing Subplots
1. Sharing X and Y Axes
You can make subplots share the same axes for easier comparison.
fig, axs = plt.subplots(2, 1, sharex=True, sharey=True)
axs[0].plot(x, y1, 'b-o')
axs[0].set_title("y = x^2")
axs[1].plot(x, y2, 'r-s')
axs[1].set_title("y = reverse(x^2)")
plt.suptitle("Shared Axes Example")
plt.show()
2. Adjusting Spacing
Sometimes subplots overlap; use plt.tight_layout()
to fix it.
plt.tight_layout(pad=2.0)
3. Figure Size
You can control the figure size with figsize
.
fig, axs = plt.subplots(1, 3, figsize=(12, 4))
📊 Real-World Example: Comparing Sales Trends
months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun"]
product_A = [500, 600, 700, 800, 750, 900]
product_B = [300, 400, 350, 500, 450, 600]
fig, axs = plt.subplots(1, 2, figsize=(10, 4))
# Line plot for Product A
axs[0].plot(months, product_A, marker='o', color='blue')
axs[0].set_title("Product A Sales")
axs[0].set_ylabel("Units Sold")
# Line plot for Product B
axs[1].plot(months, product_B, marker='s', color='red')
axs[1].set_title("Product B Sales")
plt.suptitle("Sales Comparison Using Subplots")
plt.show()
✅ This shows two line plots side by side for comparing sales performance.
✅ Key Takeaways
- Subplots allow multiple plots in one figure for better comparisons.
plt.subplot()
is simple but less flexible.plt.subplots()
is more powerful and recommended for professional use.- You can customize layout, share axes, and set figure sizes.
- Great for comparing datasets or visualizing multiple perspectives at once.