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