48
48
plt .rcParams ['savefig.facecolor' ] = "0.8"
49
49
plt .rcParams ['figure.figsize' ] = 4.5 , 4.
50
50
51
-
52
51
def example_plot (ax , fontsize = 12 , nodec = False ):
53
52
ax .plot ([1 , 2 ])
54
53
@@ -62,7 +61,7 @@ def example_plot(ax, fontsize=12, nodec=False):
62
61
ax .set_yticklabels ('' )
63
62
64
63
65
- fig , ax = plt .subplots ()
64
+ fig , ax = plt .subplots (constrained_layout = False )
66
65
example_plot (ax , fontsize = 24 )
67
66
68
67
###############################################################################
@@ -285,8 +284,10 @@ def example_plot(ax, fontsize=12, nodec=False):
285
284
# with :func:`~matplotlib.figure.Figure.subplots` or
286
285
# :func:`~matplotlib.gridspec.GridSpec` and
287
286
# :func:`~matplotlib.figure.Figure.add_subplot`.
287
+ #
288
+ # Note that in what follows ``constrained_layout=True``
288
289
289
- fig = plt .figure (constrained_layout = True )
290
+ fig = plt .figure ()
290
291
291
292
gs1 = gridspec .GridSpec (2 , 1 , figure = fig )
292
293
ax1 = fig .add_subplot (gs1 [0 ])
@@ -296,20 +297,21 @@ def example_plot(ax, fontsize=12, nodec=False):
296
297
example_plot (ax2 )
297
298
298
299
###############################################################################
299
- # More complicated gridspec layouts are possible...
300
+ # More complicated gridspec layouts are possible. Note here we use the
301
+ # convenenience functions ``add_gridspec`` and ``subgridspec``
300
302
301
- fig = plt .figure (constrained_layout = True )
303
+ fig = plt .figure ()
302
304
303
- gs0 = gridspec . GridSpec (1 , 2 , figure = fig )
305
+ gs0 = fig . add_gridspec (1 , 2 )
304
306
305
- gs1 = gridspec . GridSpecFromSubplotSpec (2 , 1 , gs0 [ 0 ] )
307
+ gs1 = gs0 [ 0 ]. subgridspec (2 , 1 )
306
308
ax1 = fig .add_subplot (gs1 [0 ])
307
309
ax2 = fig .add_subplot (gs1 [1 ])
308
310
309
311
example_plot (ax1 )
310
312
example_plot (ax2 )
311
313
312
- gs2 = gridspec . GridSpecFromSubplotSpec (3 , 1 , gs0 [ 1 ] )
314
+ gs2 = gs0 [ 1 ]. subgridspec (3 , 1 )
313
315
314
316
for ss in gs2 :
315
317
ax = fig .add_subplot (ss )
@@ -324,9 +326,9 @@ def example_plot(ax, fontsize=12, nodec=False):
324
326
# extent. If we want the top and bottom of the two grids to line up then
325
327
# they need to be in the same gridspec:
326
328
327
- fig = plt .figure (constrained_layout = True )
329
+ fig = plt .figure ()
328
330
329
- gs0 = gridspec . GridSpec (6 , 2 , figure = fig )
331
+ gs0 = fig . add_gridspec (6 , 2 )
330
332
331
333
ax1 = fig .add_subplot (gs0 [:3 , 0 ])
332
334
ax2 = fig .add_subplot (gs0 [3 :, 0 ])
@@ -349,10 +351,10 @@ def example_plot(ax, fontsize=12, nodec=False):
349
351
350
352
351
353
def docomplicated (suptitle = None ):
352
- fig = plt .figure (constrained_layout = True )
353
- gs0 = gridspec . GridSpec (1 , 2 , figure = fig , width_ratios = [1. , 2. ])
354
- gsl = gridspec . GridSpecFromSubplotSpec (2 , 1 , gs0 [ 0 ] )
355
- gsr = gridspec . GridSpecFromSubplotSpec (2 , 2 , gs0 [ 1 ] )
354
+ fig = plt .figure ()
355
+ gs0 = fig . add_gridspec (1 , 2 , figure = fig , width_ratios = [1. , 2. ])
356
+ gsl = gs0 [ 0 ]. subgridspec (2 , 1 )
357
+ gsr = gs0 [ 1 ]. subgridspec (2 , 2 )
356
358
357
359
for gs in gsl :
358
360
ax = fig .add_subplot (gs )
@@ -381,7 +383,7 @@ def docomplicated(suptitle=None):
381
383
# effect on it anymore. (Note that constrained_layout still leaves the space
382
384
# for the axes that is moved).
383
385
384
- fig , axs = plt .subplots (1 , 2 , constrained_layout = True )
386
+ fig , axs = plt .subplots (1 , 2 )
385
387
example_plot (axs [0 ], fontsize = 12 )
386
388
axs [1 ].set_position ([0.2 , 0.2 , 0.4 , 0.4 ])
387
389
@@ -395,7 +397,7 @@ def docomplicated(suptitle=None):
395
397
396
398
from matplotlib .transforms import Bbox
397
399
398
- fig , axs = plt .subplots (1 , 2 , constrained_layout = True )
400
+ fig , axs = plt .subplots (1 , 2 )
399
401
example_plot (axs [0 ], fontsize = 12 )
400
402
fig .execute_constrained_layout ()
401
403
# put into data-space:
@@ -419,7 +421,7 @@ def docomplicated(suptitle=None):
419
421
# to yield a nice layout:
420
422
421
423
422
- fig = plt .figure (constrained_layout = True )
424
+ fig = plt .figure ()
423
425
424
426
ax1 = plt .subplot (221 )
425
427
ax2 = plt .subplot (223 )
@@ -432,8 +434,8 @@ def docomplicated(suptitle=None):
432
434
###############################################################################
433
435
# Of course that layout is possible using a gridspec:
434
436
435
- fig = plt .figure (constrained_layout = True )
436
- gs = gridspec . GridSpec (2 , 2 , figure = fig )
437
+ fig = plt .figure ()
438
+ gs = fig . add_gridspec (2 , 2 )
437
439
438
440
ax1 = fig .add_subplot (gs [0 , 0 ])
439
441
ax2 = fig .add_subplot (gs [1 , 0 ])
@@ -448,7 +450,7 @@ def docomplicated(suptitle=None):
448
450
# :func:`~matplotlib.pyplot.subplot2grid` doesn't work for the same reason:
449
451
# each call creates a different parent gridspec.
450
452
451
- fig = plt .figure (constrained_layout = True )
453
+ fig = plt .figure ()
452
454
453
455
ax1 = plt .subplot2grid ((3 , 3 ), (0 , 0 ))
454
456
ax2 = plt .subplot2grid ((3 , 3 ), (0 , 1 ), colspan = 2 )
@@ -464,8 +466,8 @@ def docomplicated(suptitle=None):
464
466
# The way to make this plot compatible with ``constrained_layout`` is again
465
467
# to use ``gridspec`` directly
466
468
467
- fig = plt .figure (constrained_layout = True )
468
- gs = gridspec . GridSpec (3 , 3 , figure = fig )
469
+ fig = plt .figure ()
470
+ gs = fig . add_gridspec (3 , 3 )
469
471
470
472
ax1 = fig .add_subplot (gs [0 , 0 ])
471
473
ax2 = fig .add_subplot (gs [0 , 1 :])
0 commit comments