100100import numpy as np
101101
102102fig , axs = plt .subplots (ncols = 2 , nrows = 2 , figsize = (5.5 , 3.5 ),
103- constrained_layout = True )
103+ layout = "constrained" )
104104# add an artist, in this case a nice label in the middle...
105105for row in range (2 ):
106106 for col in range (2 ):
@@ -129,11 +129,41 @@ def annotate_axes(ax, text, fontsize=18):
129129
130130fig , axd = plt .subplot_mosaic ([['upper left' , 'upper right' ],
131131 ['lower left' , 'lower right' ]],
132- figsize = (5.5 , 3.5 ), constrained_layout = True )
132+ figsize = (5.5 , 3.5 ), layout = "constrained" )
133133for k in axd :
134134 annotate_axes (axd [k ], f'axd["{ k } "]' , fontsize = 14 )
135135fig .suptitle ('plt.subplot_mosaic()' )
136136
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+
137167############################################################################
138168# Axes spanning rows or columns in a grid
139169# ---------------------------------------
@@ -145,7 +175,7 @@ def annotate_axes(ax, text, fontsize=18):
145175
146176fig , axd = plt .subplot_mosaic ([['upper left' , 'right' ],
147177 ['lower left' , 'right' ]],
148- figsize = (5.5 , 3.5 ), constrained_layout = True )
178+ figsize = (5.5 , 3.5 ), layout = "constrained" )
149179for k in axd :
150180 annotate_axes (axd [k ], f'axd["{ k } "]' , fontsize = 14 )
151181fig .suptitle ('plt.subplot_mosaic()' )
@@ -168,7 +198,7 @@ def annotate_axes(ax, text, fontsize=18):
168198fig , axd = plt .subplot_mosaic ([['upper left' , 'right' ],
169199 ['lower left' , 'right' ]],
170200 gridspec_kw = gs_kw , figsize = (5.5 , 3.5 ),
171- constrained_layout = True )
201+ layout = "constrained" )
172202for k in axd :
173203 annotate_axes (axd [k ], f'axd["{ k } "]' , fontsize = 14 )
174204fig .suptitle ('plt.subplot_mosaic()' )
@@ -184,7 +214,7 @@ def annotate_axes(ax, text, fontsize=18):
184214# necessarily aligned. See below for a more verbose way to achieve the same
185215# effect with `~.gridspec.GridSpecFromSubplotSpec`.
186216
187- fig = plt .figure (constrained_layout = True )
217+ fig = plt .figure (layout = "constrained" )
188218subfigs = fig .subfigures (1 , 2 , wspace = 0.07 , width_ratios = [1.5 , 1. ])
189219axs0 = subfigs [0 ].subplots (2 , 2 )
190220subfigs [0 ].set_facecolor ('0.9' )
@@ -207,7 +237,7 @@ def annotate_axes(ax, text, fontsize=18):
207237outer = [['upper left' , inner ],
208238 ['lower left' , 'lower right' ]]
209239
210- fig , axd = plt .subplot_mosaic (outer , constrained_layout = True )
240+ fig , axd = plt .subplot_mosaic (outer , layout = "constrained" )
211241for k in axd :
212242 annotate_axes (axd [k ], f'axd["{ k } "]' )
213243
@@ -230,7 +260,7 @@ def annotate_axes(ax, text, fontsize=18):
230260# We can accomplish a 2x2 grid in the same manner as
231261# ``plt.subplots(2, 2)``:
232262
233- fig = plt .figure (figsize = (5.5 , 3.5 ), constrained_layout = True )
263+ fig = plt .figure (figsize = (5.5 , 3.5 ), layout = "constrained" )
234264spec = fig .add_gridspec (ncols = 2 , nrows = 2 )
235265
236266ax0 = fig .add_subplot (spec [0 , 0 ])
@@ -256,7 +286,7 @@ def annotate_axes(ax, text, fontsize=18):
256286# and the new Axes will span the slice. This would be the same
257287# as ``fig, axd = plt.subplot_mosaic([['ax0', 'ax0'], ['ax1', 'ax2']], ...)``:
258288
259- fig = plt .figure (figsize = (5.5 , 3.5 ), constrained_layout = True )
289+ fig = plt .figure (figsize = (5.5 , 3.5 ), layout = "constrained" )
260290spec = fig .add_gridspec (2 , 2 )
261291
262292ax0 = fig .add_subplot (spec [0 , :])
@@ -284,7 +314,7 @@ def annotate_axes(ax, text, fontsize=18):
284314# These spacing parameters can also be passed to `~.pyplot.subplots` and
285315# `~.pyplot.subplot_mosaic` as the *gridspec_kw* argument.
286316
287- fig = plt .figure (constrained_layout = False , facecolor = '0.9' )
317+ fig = plt .figure (layout = None , facecolor = '0.9' )
288318gs = fig .add_gridspec (nrows = 3 , ncols = 3 , left = 0.05 , right = 0.75 ,
289319 hspace = 0.1 , wspace = 0.05 )
290320ax0 = fig .add_subplot (gs [:- 1 , :])
@@ -306,7 +336,7 @@ def annotate_axes(ax, text, fontsize=18):
306336# Note this is also available from the more verbose
307337# `.gridspec.GridSpecFromSubplotSpec`.
308338
309- fig = plt .figure (constrained_layout = True )
339+ fig = plt .figure (layout = "constrained" )
310340gs0 = fig .add_gridspec (1 , 2 )
311341
312342gs00 = gs0 [0 ].subgridspec (2 , 2 )
0 commit comments