r/matplotlib • • Feb 09 '20

Why subplot(1,1,1)

Hi, I know that by using subplot, we can create a figure with several subplots. Supposing that we have (2,3,1), it tells that in the figure, there are 6 subplots in the form of 2x3 (2 rows, 3 columns of subplots). The last element indicates which subplot is of interest. In this case, it is the one on the top left of the figure.

If there is only one plot in the figure, why somebody uses something like:

import matplotlib.pyplot as plt

fig = plt.figure(); ax = fig.add_subplot(1,1,1)

2 Upvotes

3 comments sorted by

View all comments

Show parent comment

2

u/[deleted] Mar 04 '20 edited Mar 04 '20

Yes. Also, at the risk of dumping to much information here, I find myself using GridSpec a lot (for complex plots). It works similar to plt.subplots but for one extra import line you get the ability to make irregular grids of plots, by doing for example:

import matplotlib.pyplot as plt
from matplotlib.gridspec import GridSpec
fig = plt.figure()
subgrid = GridSpec(2, 2)
ax_top_left = fig.add_subplot(subgrid[0, 0])
ax_top_right = fig.add_subplot(subgrid[0, 1])
ax_bottom = fig.add_subplot(subgrid[1, :])
plt.show()

You can see that this uses the standard python (numpy) slice notation, so the bottom plot spans all of the second row. The demo is here and a more advanced tutorial is available. Anyway, that's a bit more complex that just one plot ;)

edit: fixed example