Subplots
Subplots allow you to place multiple charts inside one figure. They are useful when you want to compare different visualizations together.
A subplot is a smaller chart inside one larger figure.
Instead of creating several separate charts, you can
arrange multiple charts together using
plt.subplots().
What Is a Subplot?
Imagine you want to display three different charts:
Chart 1 → Sales Chart 2 → Expenses Chart 3 → Profit
You could create three separate figures. But it is often better to put them into one figure.
+------------------+------------------+ | | | | Sales | Expenses | | | | +------------------+------------------+ | | | Profit | | | +-------------------------------------+
Each individual chart is called a subplot.
Figure vs Subplot
This distinction is important.
Figure ↓ The entire canvas Subplot ↓ One chart inside that canvas
Think about a piece of paper.
Figure = entire paper Subplot = one section of the paper
A single figure can contain multiple subplots.
Create Your First Subplot
The easiest way is to use
plt.subplots().
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
ax.plot(
[1, 2, 3, 4],
[10, 20, 30, 40]
)
plt.show()
Here:
fig ↓ The complete figure ax ↓ The subplot / chart area
Create Two Subplots
Suppose we want two charts side by side.
import matplotlib.pyplot as plt
fig, ax = plt.subplots(
1,
2
)
ax[0].plot(
[1, 2, 3, 4],
[10, 20, 30, 40]
)
ax[1].plot(
[1, 2, 3, 4],
[40, 30, 20, 10]
)
plt.show()
The first number means rows and the second means columns.
plt.subplots(
rows,
columns
)
Therefore:
plt.subplots(1, 2) means: 1 row 2 columns
Visually:
+----------------+----------------+ | | | | Chart 1 | Chart 2 | | | | +----------------+----------------+
Understanding ax[0] and ax[1]
When there are multiple subplots, Matplotlib gives you multiple axes objects.
fig, ax = plt.subplots(1, 2)
You can think of them as:
ax[0] ↓ First subplot ax[1] ↓ Second subplot
Therefore:
ax[0].plot(...)
↓
Draw on first chart
ax[1].plot(...)
↓
Draw on second chart
Subplots in Two Rows
You can also arrange charts vertically.
import matplotlib.pyplot as plt
fig, ax = plt.subplots(
2,
1
)
ax[0].plot(
[1, 2, 3, 4],
[10, 20, 30, 40]
)
ax[1].plot(
[1, 2, 3, 4],
[40, 30, 20, 10]
)
plt.show()
The layout is:
+------------------+ | | | Chart 1 | | | +------------------+ | | | Chart 2 | | | +------------------+
Because we used:
plt.subplots(2, 1) 2 rows 1 column
Create a 2 × 2 Grid
You can create four charts using two rows and two columns.
import matplotlib.pyplot as plt
fig, ax = plt.subplots(
2,
2
)
ax[0, 0].plot(
[1, 2, 3],
[10, 20, 30]
)
ax[0, 1].bar(
["A", "B", "C"],
[20, 35, 25]
)
ax[1, 0].scatter(
[1, 2, 3],
[30, 20, 40]
)
ax[1, 1].plot(
[1, 2, 3],
[40, 30, 20]
)
plt.show()
Now ax is a two-dimensional structure.
ax[0, 0] → top-left ax[0, 1] → top-right ax[1, 0] → bottom-left ax[1, 1] → bottom-right
Think of the positions like a table:
Column
0 1
Row 0 [0,0] [0,1]
Row 1 [1,0] [1,1]
Give Each Subplot a Title
Each subplot can have its own title.
import matplotlib.pyplot as plt
fig, ax = plt.subplots(1, 2)
ax[0].plot(
[1, 2, 3, 4],
[10, 20, 30, 40]
)
ax[0].set_title(
"Sales"
)
ax[1].plot(
[1, 2, 3, 4],
[40, 30, 20, 10]
)
ax[1].set_title(
"Expenses"
)
plt.show()
Notice the difference:
plt.title()
↓
Common pyplot approach
ax[0].set_title()
↓
Specific subplot
When working with multiple subplots, using the
ax object is clearer.
Add Axis Labels
Each subplot can also have its own X and Y labels.
import matplotlib.pyplot as plt
fig, ax = plt.subplots(1, 2)
ax[0].plot(
[1, 2, 3, 4],
[10, 20, 30, 40]
)
ax[0].set_title("Sales")
ax[0].set_xlabel("Month")
ax[0].set_ylabel("Sales")
ax[1].plot(
[1, 2, 3, 4],
[40, 30, 20, 10]
)
ax[1].set_title("Expenses")
ax[1].set_xlabel("Month")
ax[1].set_ylabel("Expenses")
plt.show()
Different Chart Types in One Figure
One of the most useful things about subplots is that different chart types can be displayed together.
import matplotlib.pyplot as plt
fig, ax = plt.subplots(1, 3)
# Line chart
ax[0].plot(
[1, 2, 3, 4],
[10, 20, 30, 40]
)
ax[0].set_title("Line Chart")
# Bar chart
ax[1].bar(
["A", "B", "C"],
[20, 35, 25]
)
ax[1].set_title("Bar Chart")
# Scatter plot
ax[2].scatter(
[1, 2, 3, 4],
[15, 25, 20, 40]
)
ax[2].set_title("Scatter Plot")
plt.show()
The result is conceptually:
+-------------+-------------+-------------+ | Line Chart | Bar Chart | Scatter | | | | Plot | +-------------+-------------+-------------+
Real-World Example
Imagine you have monthly business data:
months = [
"Jan",
"Feb",
"Mar",
"Apr",
"May"
]
sales = [
100,
120,
150,
140,
180
]
expenses = [
70,
80,
90,
95,
110
]
You may want to see sales and expenses together.
import matplotlib.pyplot as plt
months = [
"Jan",
"Feb",
"Mar",
"Apr",
"May"
]
sales = [
100,
120,
150,
140,
180
]
expenses = [
70,
80,
90,
95,
110
]
fig, ax = plt.subplots(1, 2)
ax[0].plot(
months,
sales
)
ax[0].set_title(
"Monthly Sales"
)
ax[0].set_xlabel(
"Month"
)
ax[0].set_ylabel(
"Sales"
)
ax[1].plot(
months,
expenses
)
ax[1].set_title(
"Monthly Expenses"
)
ax[1].set_xlabel(
"Month"
)
ax[1].set_ylabel(
"Expenses"
)
plt.show()
Fix Overlapping Charts
When you have several subplots, labels and titles can sometimes overlap.
Use tight_layout() to automatically adjust
the spacing.
import matplotlib.pyplot as plt
fig, ax = plt.subplots(1, 2)
ax[0].plot(
[1, 2, 3],
[10, 20, 30]
)
ax[0].set_title(
"Sales"
)
ax[1].plot(
[1, 2, 3],
[30, 20, 10]
)
ax[1].set_title(
"Expenses"
)
plt.tight_layout()
plt.show()
The important line is:
plt.tight_layout()
It tells Matplotlib to improve the spacing between the subplots.
Control Figure Size
You can control the size of the complete figure with
figsize.
import matplotlib.pyplot as plt
fig, ax = plt.subplots(
1,
2,
figsize=(12, 5)
)
ax[0].plot(
[1, 2, 3],
[10, 20, 30]
)
ax[1].bar(
["A", "B", "C"],
[20, 30, 25]
)
plt.tight_layout()
plt.show()
Here:
figsize=(12, 5) 12 → width 5 → height
Subplots in AI and Machine Learning
Subplots become especially useful when analyzing a dataset during Exploratory Data Analysis.
For example, suppose you have machine-learning data containing house information.
house_size = [
800,
1000,
1200,
1500,
1800
]
house_price = [
150000,
180000,
220000,
280000,
340000
]
house_age = [
20,
15,
12,
8,
5
]
You could visualize different relationships together.
import matplotlib.pyplot as plt
fig, ax = plt.subplots(
1,
2,
figsize=(12, 5)
)
# House size vs price
ax[0].scatter(
house_size,
house_price
)
ax[0].set_title(
"House Size vs Price"
)
ax[0].set_xlabel(
"House Size"
)
ax[0].set_ylabel(
"House Price"
)
# House age vs price
ax[1].scatter(
house_age,
house_price
)
ax[1].set_title(
"House Age vs Price"
)
ax[1].set_xlabel(
"House Age"
)
ax[1].set_ylabel(
"House Price"
)
plt.tight_layout()
plt.show()
Now you can visually compare two relationships before building a Machine Learning model.
The Important Pattern to Remember
Don't memorize every variation of subplot code. Understand this basic pattern.
fig, ax = plt.subplots(
rows,
columns
)
Then use the appropriate position.
1 × 2 ax[0] ax[1] 2 × 1 ax[0] ax[1] 2 × 2 ax[0, 0] ax[0, 1] ax[1, 0] ax[1, 1]
Complete Example
Here is a practical example combining line, bar, and scatter charts in one figure.
import matplotlib.pyplot as plt
months = [
"Jan",
"Feb",
"Mar",
"Apr",
"May"
]
sales = [
100,
120,
150,
140,
180
]
expenses = [
70,
80,
90,
95,
110
]
profit = [
30,
40,
60,
45,
70
]
fig, ax = plt.subplots(
1,
3,
figsize=(15, 5)
)
# 1. Sales
ax[0].plot(
months,
sales
)
ax[0].set_title(
"Sales"
)
ax[0].set_xlabel(
"Month"
)
ax[0].set_ylabel(
"Sales"
)
# 2. Expenses
ax[1].bar(
months,
expenses
)
ax[1].set_title(
"Expenses"
)
ax[1].set_xlabel(
"Month"
)
ax[1].set_ylabel(
"Expenses"
)
# 3. Profit
ax[2].scatter(
months,
profit
)
ax[2].set_title(
"Profit"
)
ax[2].set_xlabel(
"Month"
)
ax[2].set_ylabel(
"Profit"
)
plt.tight_layout()
plt.show()
The mental model is simple:
One Figure
│
├── Subplot 1 → Sales
│
├── Subplot 2 → Expenses
│
└── Subplot 3 → Profit
Subplots let you compare multiple charts inside one figure.
Use plt.subplots(rows, columns) to create
the layout, then use the returned ax
objects to draw each chart. Remember:
ax[0] refers to the first subplot, while
ax[0, 1] refers to a specific row and
column in a grid. This becomes very useful in AI and
Machine Learning when comparing multiple variables,
distributions, and relationships during data analysis.