I’m trying to make 3 pairs of subplots, with more space between the pairs. But I can’t independently change the space between the pairs.
I’m using nested GridSpec
s following this answer.
from matplotlib import pyplot as plt
from matplotlib import gridspec
layout = "constrained"
fig = plt.figure(figsize=(2, 6), layout=layout)
fig.suptitle(f"{layout=}")
gs = gridspec.GridSpec(3, 1, figure=fig, hspace=0.2)
for g in gs:
sub_gs = g.subgridspec(2, 1, hspace=0.1)
for gg in sub_gs:
ax = fig.add_subplot(gg)
ax.tick_params(labelbottom=False, labelleft=False, direction="in")
fig.savefig(f"test-{layout}.png")
plt.show()
When layout = None
the spacing makes sense (3 groups of 2 subplots); when layout = "constrained"
it doesn’t…In fact, the outer GridSpec
‘s hspace
has no effect at all when layout = "constrained"
. Instead, the outer GridSpec
‘s spacing only responds to the h_pad
of the layout engine, which affects all subplots (see Constrained Layout Guide # Padding and spacing).
How can I get the result on the left using constrained layout, so that e.g. the subplots fill the whole figure, a colorbar can easily be added spanning all 6 subplots, etc?