100
100
import numpy as np
101
101
102
102
fig , axs = plt .subplots (ncols = 2 , nrows = 2 , figsize = (5.5 , 3.5 ),
103
- constrained_layout = True )
103
+ layout = "constrained" )
104
104
# add an artist, in this case a nice label in the middle...
105
105
for row in range (2 ):
106
106
for col in range (2 ):
@@ -129,11 +129,41 @@ def annotate_axes(ax, text, fontsize=18):
129
129
130
130
fig , axd = plt .subplot_mosaic ([['upper left' , 'upper right' ],
131
131
['lower left' , 'lower right' ]],
132
- figsize = (5.5 , 3.5 ), constrained_layout = True )
132
+ figsize = (5.5 , 3.5 ), layout = "constrained" )
133
133
for k in axd :
134
134
annotate_axes (axd [k ], f'axd["{ k } "]' , fontsize = 14 )
135
135
fig .suptitle ('plt.subplot_mosaic()' )
136
136
137
+ #############################################################################
138
+ #
139
+ # Grids of fixed-aspect ratio Axes
140
+ # --------------------------------
141
+ #
142
+ # Fixed-aspect ratio axes are common for images or maps. However, they
143
+ # present a challenge to layout because two sets of constraints are being
144
+ # imposed on the size of the Axes - that they fit in the figure and that they
145
+ # have a set aspect ratio. This leads to large gaps between Axes by default:
146
+ #
147
+
148
+ fig , axs = plt .subplots (2 , 2 , layout = "constrained" , figsize = (5.5 , 3.5 ))
149
+ for ax in axs .flat :
150
+ ax .set_aspect (1 )
151
+ fig .suptitle ('Fixed aspect Axes' )
152
+
153
+ ############################################################################
154
+ # One way to address this is to change the aspect of the figure to be close
155
+ # to the aspect ratio of the Axes, however that requires trial and error.
156
+ # Matplotlib also supplies ``layout="compressed"``, which will work with
157
+ # simple grids to reduce the gaps between Axes. (The ``mpl_toolkits`` also
158
+ # provides `~.mpl_toolkits.axes_grid1.axes_grid.ImageGrid` to accomplish
159
+ # a similar effect, but with a non-standard Axes class).
160
+
161
+ fig , axs = plt .subplots (2 , 2 , layout = "compressed" , figsize = (5.5 , 3.5 ))
162
+ for ax in axs .flat :
163
+ ax .set_aspect (1 )
164
+ fig .suptitle ('Fixed aspect Axes: compressed' )
165
+
166
+
137
167
############################################################################
138
168
# Axes spanning rows or columns in a grid
139
169
# ---------------------------------------
@@ -145,7 +175,7 @@ def annotate_axes(ax, text, fontsize=18):
145
175
146
176
fig , axd = plt .subplot_mosaic ([['upper left' , 'right' ],
147
177
['lower left' , 'right' ]],
148
- figsize = (5.5 , 3.5 ), constrained_layout = True )
178
+ figsize = (5.5 , 3.5 ), layout = "constrained" )
149
179
for k in axd :
150
180
annotate_axes (axd [k ], f'axd["{ k } "]' , fontsize = 14 )
151
181
fig .suptitle ('plt.subplot_mosaic()' )
@@ -168,7 +198,7 @@ def annotate_axes(ax, text, fontsize=18):
168
198
fig , axd = plt .subplot_mosaic ([['upper left' , 'right' ],
169
199
['lower left' , 'right' ]],
170
200
gridspec_kw = gs_kw , figsize = (5.5 , 3.5 ),
171
- constrained_layout = True )
201
+ layout = "constrained" )
172
202
for k in axd :
173
203
annotate_axes (axd [k ], f'axd["{ k } "]' , fontsize = 14 )
174
204
fig .suptitle ('plt.subplot_mosaic()' )
@@ -184,7 +214,7 @@ def annotate_axes(ax, text, fontsize=18):
184
214
# necessarily aligned. See below for a more verbose way to achieve the same
185
215
# effect with `~.gridspec.GridSpecFromSubplotSpec`.
186
216
187
- fig = plt .figure (constrained_layout = True )
217
+ fig = plt .figure (layout = "constrained" )
188
218
subfigs = fig .subfigures (1 , 2 , wspace = 0.07 , width_ratios = [1.5 , 1. ])
189
219
axs0 = subfigs [0 ].subplots (2 , 2 )
190
220
subfigs [0 ].set_facecolor ('0.9' )
@@ -207,7 +237,7 @@ def annotate_axes(ax, text, fontsize=18):
207
237
outer = [['upper left' , inner ],
208
238
['lower left' , 'lower right' ]]
209
239
210
- fig , axd = plt .subplot_mosaic (outer , constrained_layout = True )
240
+ fig , axd = plt .subplot_mosaic (outer , layout = "constrained" )
211
241
for k in axd :
212
242
annotate_axes (axd [k ], f'axd["{ k } "]' )
213
243
@@ -230,7 +260,7 @@ def annotate_axes(ax, text, fontsize=18):
230
260
# We can accomplish a 2x2 grid in the same manner as
231
261
# ``plt.subplots(2, 2)``:
232
262
233
- fig = plt .figure (figsize = (5.5 , 3.5 ), constrained_layout = True )
263
+ fig = plt .figure (figsize = (5.5 , 3.5 ), layout = "constrained" )
234
264
spec = fig .add_gridspec (ncols = 2 , nrows = 2 )
235
265
236
266
ax0 = fig .add_subplot (spec [0 , 0 ])
@@ -256,7 +286,7 @@ def annotate_axes(ax, text, fontsize=18):
256
286
# and the new Axes will span the slice. This would be the same
257
287
# as ``fig, axd = plt.subplot_mosaic([['ax0', 'ax0'], ['ax1', 'ax2']], ...)``:
258
288
259
- fig = plt .figure (figsize = (5.5 , 3.5 ), constrained_layout = True )
289
+ fig = plt .figure (figsize = (5.5 , 3.5 ), layout = "constrained" )
260
290
spec = fig .add_gridspec (2 , 2 )
261
291
262
292
ax0 = fig .add_subplot (spec [0 , :])
@@ -284,7 +314,7 @@ def annotate_axes(ax, text, fontsize=18):
284
314
# These spacing parameters can also be passed to `~.pyplot.subplots` and
285
315
# `~.pyplot.subplot_mosaic` as the *gridspec_kw* argument.
286
316
287
- fig = plt .figure (constrained_layout = False , facecolor = '0.9' )
317
+ fig = plt .figure (layout = None , facecolor = '0.9' )
288
318
gs = fig .add_gridspec (nrows = 3 , ncols = 3 , left = 0.05 , right = 0.75 ,
289
319
hspace = 0.1 , wspace = 0.05 )
290
320
ax0 = fig .add_subplot (gs [:- 1 , :])
@@ -306,7 +336,7 @@ def annotate_axes(ax, text, fontsize=18):
306
336
# Note this is also available from the more verbose
307
337
# `.gridspec.GridSpecFromSubplotSpec`.
308
338
309
- fig = plt .figure (constrained_layout = True )
339
+ fig = plt .figure (layout = "constrained" )
310
340
gs0 = fig .add_gridspec (1 , 2 )
311
341
312
342
gs00 = gs0 [0 ].subgridspec (2 , 2 )
0 commit comments