A simple example for multi-row multi-column figures
import matplotlib.pyplot as plt
x = range(10)
y = range(10)
fig, axs = plt.subplots(nrows=3, ncols=3)
for row in axs:
for col in row:
col.plot(x, y)
plt.show()
Note the axs returned from subplots is 2-dimensional if row number > 1. If the row number is 1, axs is one-dimensinal. In the former case, we use axs[i, j] to plot subfigure, in the later case, we use axs[i] to plot.
Below is an exmaple for 2 x3 and 1x6 figures. See how the axs is accessed.
x = range(10)
y = range(10)
subfigures = 5
def plot_figures(rows, cols):
fig, axs = plt.subplots(nrows=rows, ncols=cols, figsize = (8, 8))
for i in range(subfigures):
row, col = int(i/cols), i%cols
if subfigures <= cols:
ax = axs[col]
else:
ax = axs[row, col]
ax.plot(x, y)
ax.set_xticks(x)
ax.set_xticklabels(x)
ax.set_title('figure {}'.format(i))
i += 1
fig.tight_layout()
plt.show()
plot_figures(2,3)
#plot_figures(1,6)