Why does adding axes to a subgridspec mess with <wspace> parameter?

133 Views Asked by At

I have a grid with 4 subplots and 2 overlaying subplots to create common xlabels between the first two and last two subplots (see code below). I get a slightly different spacing between the 2 subplots on the left compared to the two subplots on the right even though parameters are set to equal values, why?

If no overlaying axes are added, then the spacing is as expected. Below also a link with the image example.

Result of below code and example of the problem

import matplotlib.pyplot as plt
from matplotlib.gridspec import GridSpec
# make the grid
fig = plt.figure(constrained_layout=True, figsize=(8,3))
grsp = GridSpec(1,4, figure=fig, wspace=0.6, width_ratios=[1,0.05,1,0.05])

grsp_one = grsp[0,0].subgridspec(1,2, wspace=0.01)
grsp_two = grsp[0,2].subgridspec(1,2, wspace=0.01)

axs_one = grsp_one.subplots(sharey='row')
axs_two = grsp_two.subplots(sharey='row')
# create the ovelapping ax on selection of axes
axmaster_1 = fig.add_subplot(grsp_one[0,0:2])
axmaster_2 = fig.add_subplot(grsp_two[0,0:2])

for axmaster in [axmaster_1, axmaster_2]:
    # hide the ticks of the master axis
    axmaster.xaxis.set_major_locator(matplotlib.ticker.NullLocator())
    axmaster.yaxis.set_major_locator(matplotlib.ticker.NullLocator())
    axmaster.spines[['bottom', 'right', 'top', 'left']].set_visible(False)
    axmaster.set_zorder(-10)


axmaster_1.set_xlabel('xlabel_1', labelpad=20)
axmaster_2.set_xlabel('xlabel_2', labelpad=20)

axmaster_1.set_ylabel('ylabel_1', labelpad=30)
axmaster_2.set_ylabel('ylabel_2', labelpad=30)

plt.show()

1

There are 1 best solutions below

0
Stef M On

As suggested by @MattPitkin, setting constrained_layout=False with updated matplotlib version solves the issue of unequal spaces but requires some fiddling with wspace, hspace, left, bottom, right, top of the gridspec and subgridspec to make the figure good looking.