Skip to content

FIX/ENH CL: Allow single parent colorbar w/ gridspec layout #10629

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion lib/matplotlib/colorbar.py
Original file line number Diff line number Diff line change
Expand Up @@ -1124,6 +1124,7 @@ def make_axes(parents, location=None, orientation=None, fraction=0.15,
anchor = kw.pop('anchor', loc_settings['anchor'])
parent_anchor = kw.pop('panchor', loc_settings['panchor'])

parents_iterable = cbook.iterable(parents)
# turn parents into a list if it is not already. We do this w/ np
# because `plt.subplots` can return an ndarray and is natural to
# pass to `colorbar`.
Expand Down Expand Up @@ -1191,7 +1192,7 @@ def make_axes(parents, location=None, orientation=None, fraction=0.15,
# and we need to set the aspect ratio by hand...
cax.set_aspect(aspect, anchor=anchor, adjustable='box')
else:
if len(parents) == 1:
if not parents_iterable:
# this is a single axis...
ax = parents[0]
lb, lbpos = constrained_layout.layoutcolorbarsingle(
Expand Down
30 changes: 27 additions & 3 deletions tutorials/intermediate/constrainedlayout_guide.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,15 +105,39 @@ def example_plot(ax, fontsize=12, nodec=False):
fig.colorbar(im, ax=ax, shrink=0.6)

############################################################################
# If you specify multiple axes to the ``ax`` argument of ``colorbar``,
# constrained_layout will take space from all axes that share the same
# gridspec.
# If you specify a list of axes (or other iterable container) to the
# ``ax`` argument of ``colorbar``, constrained_layout will take space from all # axes that share the same gridspec.

fig, axs = plt.subplots(2, 2, figsize=(4, 4), constrained_layout=True)
for ax in axs.flatten():
im = ax.pcolormesh(arr, rasterized=True)
fig.colorbar(im, ax=axs, shrink=0.6)

############################################################################
# Note that there is a bit of a subtlety when specifying a single axes
# as the parent. In the following, it might be desirable and expected
# for the colorbars to line up, but they don't because the colorbar paired
# with the bottom axes is tied to the subplotspec of the axes, and hence
# shrinks when the gridspec-level colorbar is added.

fig, axs = plt.subplots(3, 1, figsize=(4, 4), constrained_layout=True)
for ax in axs[:2]:
im = ax.pcolormesh(arr, rasterized=True)
fig.colorbar(im, ax=axs[:2], shrink=0.6)
im = axs[2].pcolormesh(arr, rasterized=True)
fig.colorbar(im, ax=axs[2], shrink=0.6)

############################################################################
# The API to make a single-axes behave like a list of axes is to specify
# it as a list (or other iterable container), as below:

fig, axs = plt.subplots(3, 1, figsize=(4, 4), constrained_layout=True)
for ax in axs[:2]:
im = ax.pcolormesh(arr, rasterized=True)
fig.colorbar(im, ax=axs[:2], shrink=0.6)
im = axs[2].pcolormesh(arr, rasterized=True)
fig.colorbar(im, ax=[axs[2]], shrink=0.6)

####################################################
# Suptitle
# =========
Expand Down