@@ -105,7 +105,7 @@ def example_plot(ax, fontsize=12, nodec=False):
105105fig .colorbar (im , ax = ax , shrink = 0.6 )
106106
107107############################################################################
108- # If you specify multiple axes to the ``ax`` argument of ``colorbar``,
108+ # If you specify a list of axes to the ``ax`` argument of ``colorbar``,
109109# constrained_layout will take space from all axes that share the same
110110# gridspec.
111111
@@ -114,6 +114,31 @@ def example_plot(ax, fontsize=12, nodec=False):
114114 im = ax .pcolormesh (arr , rasterized = True )
115115fig .colorbar (im , ax = axs , shrink = 0.6 )
116116
117+ ############################################################################
118+ # Note that there is a bit of a subtlety when specifying a single axes
119+ # as the parent. In the following, it might be desirable and expected
120+ # for the colorbars to line up, but they don't because the colorbar paired
121+ # with the bottom axes is tied to the subplotspec of the axes, and hence
122+ # shrinks when the gridspec-level colorbar is added.
123+
124+ fig , axs = plt .subplots (3 , 1 , figsize = (4 , 4 ), constrained_layout = True )
125+ for ax in axs [:2 ]:
126+ im = ax .pcolormesh (arr , rasterized = True )
127+ fig .colorbar (im , ax = axs [:2 ], shrink = 0.6 )
128+ im = axs [2 ].pcolormesh (arr , rasterized = True )
129+ fig .colorbar (im , ax = axs [2 ], shrink = 0.6 )
130+
131+ ############################################################################
132+ # The API to make a single-axes behave like a list of axes is to specify
133+ # it as a list, as below:
134+
135+ fig , axs = plt .subplots (3 , 1 , figsize = (4 , 4 ), constrained_layout = True )
136+ for ax in axs [:2 ]:
137+ im = ax .pcolormesh (arr , rasterized = True )
138+ fig .colorbar (im , ax = axs [:2 ], shrink = 0.6 )
139+ im = axs [2 ].pcolormesh (arr , rasterized = True )
140+ fig .colorbar (im , ax = [axs [2 ]], shrink = 0.6 )
141+
117142####################################################
118143# Suptitle
119144# =========
0 commit comments