Matplotlib Drawing Multiple Plots
We can use the subplot() and subplots() method in pyplot to draw multiple subplots.
subplot() method requires specifying a position when plotting,subplots() method can generate multiple at once, and when calling, you only need to call the ax of the generated object.
subplot
subplot(nrows, ncols, index, **kwargs)
subplot(pos, **kwargs)
subplot(**kwargs)
subplot(ax)
The above functions divide the entire plotting area into nrows rows and ncols columns, then number each sub-region from left to right and top to bottom as 1...N, with the top-left sub-region numbered 1 and the bottom-right numbered N. The numbering can be set via the parameter index .
Setting numRows = 1, numCols = 2 draws the chart as a 1x2 image area, with the corresponding coordinates:
(1, 1), (1, 2)
plotNum = 1, representing the coordinates (1, 1), i.e., the subplot in the first row and first column.
plotNum = 2, representing the coordinates (1, 2), i.e., the subplot in the first row and second column.
Example
import
matplotlib.
pyplot
as
plt
import
numpy
as
np
#plot 1:
xpoints
=
np.
array
(
[
0
,
6
]
)
ypoints
=
np.
array
(
[
0
,
100
]
)
plt.
subplot
(
1
,
2
,
1
)
plt.
plot
(
xpoints
,
ypoints
)
plt.
title
(
"plot 1"
)
#plot 2:
x
=
np.
array
(
[
1
,
2
,
3
,
4
]
)
y
=
np.
array
(
[
1
,
4
,
9
,
16
]
)
plt.
subplot
(
1
,
2
,
2
)
plt.
plot
(
x
,
y
)
plt.
title
(
"plot 2"
)
plt.
suptitle
(
"RUNOOB subplot Test"
)
plt.
show
(
)
The display result is as follows:

Setting numRows = 2, numCols = 2 draws the chart as a 2x2 image area, with the corresponding coordinates:
(1, 1), (1, 2)
(2, 1), (2, 2)
plotNum = 1, representing the coordinates (1, 1), i.e., the subplot in the first row and first column.
plotNum = 2, representing the coordinates (1, 2), i.e., the subplot in the first row and second column.
plotNum = 3, representing the coordinates (2, 1), i.e., the subplot in the second row and first column.
plotNum = 4, representing the coordinates (2, 2), i.e., the subplot in the second row and second column.
Example
import
matplotlib.
pyplot
as
plt
import
numpy
as
np
#plot 1:
x
=
np.
array
(
[
0
,
6
]
)
y
=
np.
array
(
[
0
,
100
]
)
plt.
subplot
(
2
,
2
,
1
)
plt.
plot
(
x
,
y
)
plt.
title
(
"plot 1"
)
#plot 2:
x
=
np.
array
(
[
1
,
2
,
3
,
4
]
)
y
=
np.
array
(
[
1
,
4
,
9
,
16
]
)
plt.
subplot
(
2
,
2
,
2
)
plt.
plot
(
x
,
y
)
plt.
title
(
"plot 2"
)
#plot 3:
x
=
np.
array
(
[
1
,
2
,
3
,
4
]
)
y
=
np.
array
(
[
3
,
5
,
7
,
9
]
)
plt.
subplot
(
2
,
2
,
3
)
plt.
plot
(
x
,
y
)
plt.
title
(
"plot 3"
)
#plot 4:
x
=
np.
array
(
[
1
,
2
,
3
,
4
]
)
y
=
np.
array
(
[
4
,
5
,
6
,
7
]
)
plt.
subplot
(
2
,
2
,
4
)
plt.
plot
(
x
,
y
)
plt.
title
(
"plot 4"
)
plt.
suptitle
(
"RUNOOB subplot Test"
)
plt.
show
(
)
The display result is as follows:

subplots()
The syntax of the subplots() method is as follows:
matplotlib.pyplot.subplots(nrows=1, ncols=1, *, sharex=False, sharey=False, squeeze=True, subplot_kw=None, gridspec_kw=None, **fig_kw)
Parameter Description:
- nrows: default 1, sets the number of rows in the chart.
- ncols: default 1, sets the number of columns in the chart.
- sharex、sharey: sets whether the x and y axes share properties, default false, can be set to 'none', 'all', 'row' or 'col'. False or none: each subplot's x or y axis is independent. True or 'all': all subplots share the x or y axis. 'row': each row of subplots shares one x or y axis. 'col': each column of subplots shares one x or y axis.
- squeeze: boolean, default True, indicates that extra dimensions are squeezed out of the returned Axes object. For N1 or 1N subplots, a 1D array is returned; for N*M with N>1 and M>1, a 2D array is returned. If set to False, no squeezing is performed, returning a 2D array whose elements are Axes instances, even if it ends up being 1x1.
- subplot_kw: optional, dictionary type. Passes the dictionary's keywords to add_subplot() to create each subplot.
- gridspec_kw: optional, dictionary type. Passes the dictionary's keywords to the GridSpec constructor to create subplots placed in a grid.
- **fig_kw: passes detailed keyword arguments to the figure() function.
Example
import
matplotlib.
pyplot
as
plt
import
numpy
as
np
Create some test data -- Figure 1
x
=
np.
linspace
(
0
,
2
*np.
pi
,
400
)
y
=
np.
sin
(
x**
2
)
Create a figure and subplot -- Figure 2
fig
,
ax
=
plt.
subplots
(
)
ax.
plot
(
x
,
y
)
ax.
set_title
(
'Simple plot'
)
Create two subplots -- Figure 3
f
,
(
ax1
,
ax2
)
=
plt.
subplots
(
1
,
2
,
sharey
=
True
)
ax1.
plot
(
x
,
y
)
ax1.
set_title
(
'Sharing Y axis'
)
ax2.
scatter
(
x
,
y
)
Create four subplots -- Figure 4
fig
,
axs
=
plt.
subplots
(
2
,
2
,
subplot_kw
=
dict
(
projection
=
"polar"
)
)
axs
[
0
,
0
]
.
plot
(
x
,
y
)
axs
[
1
,
1
]
.
scatter
(
x
,
y
)
Shared x axis
plt.
subplots
(
2
,
2
,
sharex
=
'col'
)
Shared y axis
plt.
subplots
(
2
,
2
,
sharey
=
'row'
)
Shared x and y axes
plt.
subplots
(
2
,
2
,
sharex
=
'all'
,
sharey
=
'all'
)
This one also shares the x and y axes
plt.
subplots
(
2
,
2
,
sharex
=
True
,
sharey
=
True
)
Create a figure with id 10, deleting it if it already exists
fig
,
ax
=
plt.
subplots
(
num
=
10
,
clear
=
True
)
plt.
show
(
)
Some of the chart display results are as follows:
Figure 1

Figure 2

Figure 3

Figure 4
