Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 6a70f48

Browse files
committed
DOC: update CL tutorial to discuss colorbar layout
1 parent c17a27c commit 6a70f48

File tree

1 file changed

+28
-2
lines changed

1 file changed

+28
-2
lines changed

tutorials/intermediate/constrainedlayout_guide.py

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,14 +133,25 @@ def example_plot(ax, fontsize=12, nodec=False):
133133

134134
############################################################################
135135
# If you specify a list of axes (or other iterable container) to the
136-
# ``ax`` argument of ``colorbar``, constrained_layout will take space from all
137-
# axes that share the same gridspec.
136+
# ``ax`` argument of ``colorbar``, constrained_layout will take space from
137+
# the specified axes.
138138

139139
fig, axs = plt.subplots(2, 2, figsize=(4, 4), constrained_layout=True)
140140
for ax in axs.flatten():
141141
im = ax.pcolormesh(arr, **pc_kwargs)
142142
fig.colorbar(im, ax=axs, shrink=0.6)
143143

144+
############################################################################
145+
# If you specify a list of axes from inside a grid of axes, the colorbar
146+
# will steal space appropriately, and leave a gap, but all subplots will
147+
# still be the same size.
148+
149+
fig, axs = plt.subplots(3, 3, figsize=(4, 4), constrained_layout=True)
150+
for ax in axs.flatten():
151+
im = ax.pcolormesh(arr, **pc_kwargs)
152+
fig.colorbar(im, ax=axs[1:,][:, 1], shrink=0.8)
153+
fig.colorbar(im, ax=axs[:, -1], shrink=0.6)
154+
144155
############################################################################
145156
# Note that there is a bit of a subtlety when specifying a single axes
146157
# as the parent. In the following, it might be desirable and expected
@@ -454,6 +465,21 @@ def docomplicated(suptitle=None):
454465
bb_ax2 = Bbox(fig_coords_ax2)
455466
ax2 = fig.add_axes(bb_ax2)
456467

468+
###############################################################################
469+
# Manually turning off ``constrained_layout``
470+
# ===========================================
471+
#
472+
# ``constrained_layout`` usually adjusts the axes positions on each draw
473+
# of the figure. If you want to get the spacing provided by
474+
# ``constrained_layout`` but then not have it update, then do the initial
475+
# draw and then call ``fig.set_constrained_layout(False)``.
476+
# This is potentially useful for animations where the tick labels may
477+
# change length.
478+
#
479+
# Note that ``constrained_layout`` is turned off for ``ZOOM`` and ``PAN``
480+
# GUI events for the backends that use the toolbar. This prevents the
481+
# axes from changing position during zooming and panning.
482+
#
457483
###############################################################################
458484
# Limitations
459485
# ========================

0 commit comments

Comments
 (0)