@@ -205,11 +205,10 @@ def example_plot(ax, fontsize=12, nodec=False):
205
205
#############################################
206
206
# However, this will steal space from a subplot layout:
207
207
208
- fig , axs = plt .subplots (2 , 2 , constrained_layout = True )
209
- for ax in axs .flatten ()[:- 1 ]:
210
- ax .plot (np .arange (10 ))
211
- axs [1 , 1 ].plot (np .arange (10 ), label = 'This is a plot' )
212
- axs [1 , 1 ].legend (loc = 'center left' , bbox_to_anchor = (0.8 , 0.5 ))
208
+ fig , axs = plt .subplots (1 , 2 , figsize = (4 , 2 ), constrained_layout = True )
209
+ axs [0 ].plot (np .arange (10 ))
210
+ axs [1 ].plot (np .arange (10 ), label = 'This is a plot' )
211
+ axs [1 ].legend (loc = 'center left' , bbox_to_anchor = (0.8 , 0.5 ))
213
212
214
213
#############################################
215
214
# In order for a legend or other artist to *not* steal space
@@ -218,30 +217,47 @@ def example_plot(ax, fontsize=12, nodec=False):
218
217
# cropped, but can be useful if the plot is subsequently called
219
218
# with ``fig.savefig('outname.png', bbox_inches='tight')``. Note,
220
219
# however, that the legend's ``get_in_layout`` status will have to be
221
- # toggled again to make the saved file work:
220
+ # toggled again to make the saved file work, and we must manually
221
+ # trigger a draw if we want constrained_layout to adjust the size
222
+ # of the axes before printing.
222
223
223
- fig , axs = plt .subplots (2 , 2 , constrained_layout = True )
224
- for ax in axs . flatten ()[: - 1 ]:
225
- ax .plot (np .arange (10 ))
226
- axs [1 , 1 ].plot (np .arange (10 ), label = 'This is a plot' )
227
- leg = axs [1 , 1 ].legend (loc = 'center left' , bbox_to_anchor = (0.8 , 0.5 ))
224
+ fig , axs = plt .subplots (1 , 2 , figsize = ( 4 , 2 ) , constrained_layout = True )
225
+
226
+ axs [ 0 ] .plot (np .arange (10 ))
227
+ axs [1 ].plot (np .arange (10 ), label = 'This is a plot' )
228
+ leg = axs [1 ].legend (loc = 'center left' , bbox_to_anchor = (0.8 , 0.5 ))
228
229
leg .set_in_layout (False )
229
- wanttoprint = False
230
- if wanttoprint :
231
- leg .set_in_layout (True )
232
- fig .do_constrained_layout (False )
233
- fig .savefig ('outname.png' , bbox_inches = 'tight' )
230
+ # trigger a draw so that constrained_layout is executed once
231
+ # before we turn it off when printing....
232
+ fig .canvas .draw ()
233
+ # we want the legend included in the bbox_inches='tight' calcs.
234
+ leg .set_in_layout (True )
235
+ # we don't want the layout to change at this point.
236
+ fig .set_constrained_layout (False )
237
+ fig .savefig ('CL01.png' , bbox_inches = 'tight' , dpi = 100 )
234
238
235
239
#############################################
240
+ # The saved file looks like:
241
+ #
242
+ # .. image:: /_static/constrained_layout/CL01.png
243
+ # :align: center
244
+ #
236
245
# A better way to get around this awkwardness is to simply
237
- # use a legend for the figure:
238
- fig , axs = plt .subplots (2 , 2 , constrained_layout = True )
239
- for ax in axs .flatten ()[:- 1 ]:
240
- ax .plot (np .arange (10 ))
241
- lines = axs [1 , 1 ].plot (np .arange (10 ), label = 'This is a plot' )
246
+ # use the legend method provided by `.Figure.legend`:
247
+ fig , axs = plt .subplots (1 , 2 , figsize = (4 , 2 ), constrained_layout = True )
248
+ axs [0 ].plot (np .arange (10 ))
249
+ lines = axs [1 ].plot (np .arange (10 ), label = 'This is a plot' )
242
250
labels = [l .get_label () for l in lines ]
243
251
leg = fig .legend (lines , labels , loc = 'center left' ,
244
- bbox_to_anchor = (0.8 , 0.5 ), bbox_transform = axs [1 , 1 ].transAxes )
252
+ bbox_to_anchor = (0.8 , 0.5 ), bbox_transform = axs [1 ].transAxes )
253
+ fig .savefig ('CL02.png' , bbox_inches = 'tight' , dpi = 100 )
254
+
255
+ #############################################
256
+ # The saved file looks like:
257
+ #
258
+ # .. image:: /_static/constrained_layout/CL02.png
259
+ # :align: center
260
+ #
245
261
246
262
###############################################################################
247
263
# Padding and Spacing
0 commit comments