Description
Problem
Sometimes, trying to stick a colorbar next to its "parent" axes by using pad=0 will "fail" if the axes has a ticklabel that extends beyond the axes' spines:
from pylab import *
fig = figure(layout="constrained", figsize=(8, 4))
axs = fig.subplots(1, 2)
for i, ax in enumerate(axs):
im = ax.imshow(np.arange(9).reshape((3, 3)), aspect="auto")
fig.colorbar(im, pad=0, ticks=[])
axs[0].set(xlim=(-.5, 2.5), xticks=[0, 1, 2], yticks=[])
axs[1].set(xlim=(0, 3), xticks=[0, 1, 2, 3], yticks=[])
show()
see the small extra spacing between the left axes and the left colorbar, not present on the right.
Proposed solution
I'm not sure this can reliably be handled automatically (the desirable behavior also depends on whether the colorbar's vertical extent goes goes beyond the axes' vertical extent, which will depend on how exactly the colorbar is positioned, whether the parent axes uses fixed aspect (here I disabled fixed aspect ratio on imshow).
But it would be nice if one could at least tweak that manually with something like axs[1].xaxis.set_in_layout(False)
. Bonus points if one can do set_in_layout("y")
, i.e. take the xaxis in account but only for its vertical extent (we don't want the labels to fall off below the figure), not for its horizontal extent.