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