@@ -492,40 +492,42 @@ def docomplicated(suptitle=None):
492
492
# Incompatible functions
493
493
# ----------------------
494
494
#
495
- # ``constrained_layout`` will not work on subplots created via
496
- # `.pyplot.subplot`. The reason is that each call to `.pyplot.subplot` creates
497
- # a separate `.GridSpec` instance and ``constrained_layout`` uses (nested)
498
- # gridspecs to carry out the layout. So the following fails to yield a nice
499
- # layout:
495
+ # ``constrained_layout`` will work with `.pyplot.subplot`, but only if the
496
+ # number of rows and columns is the same for each call.
497
+ # The reason is that each call to `.pyplot.subplot` will create a new
498
+ # `.GridSpec` instance if the geometry is not the same, and
499
+ # ``constrained_layout``. So the following works fine:
500
+
500
501
501
502
fig = plt .figure ()
502
503
503
- ax1 = plt .subplot (221 )
504
- ax2 = plt .subplot (223 )
505
- ax3 = plt .subplot (122 )
504
+ ax1 = plt .subplot (2 , 2 , 1 )
505
+ ax2 = plt .subplot (2 , 2 , 3 )
506
+ ax3 = plt .subplot (2 , 2 , ( 2 , 4 ) )
506
507
507
508
example_plot (ax1 )
508
509
example_plot (ax2 )
509
510
example_plot (ax3 )
511
+ plt .suptitle ('Homogenous nrows, ncols' )
510
512
511
513
###############################################################################
512
- # Of course that layout is possible using a gridspec :
514
+ # but the following leads to a poor layout :
513
515
514
516
fig = plt .figure ()
515
- gs = fig .add_gridspec (2 , 2 )
516
517
517
- ax1 = fig . add_subplot ( gs [ 0 , 0 ] )
518
- ax2 = fig . add_subplot ( gs [ 1 , 0 ] )
519
- ax3 = fig . add_subplot ( gs [:, 1 ] )
518
+ ax1 = plt . subplot ( 2 , 2 , 1 )
519
+ ax2 = plt . subplot ( 2 , 2 , 3 )
520
+ ax3 = plt . subplot ( 1 , 2 , 2 )
520
521
521
522
example_plot (ax1 )
522
523
example_plot (ax2 )
523
524
example_plot (ax3 )
525
+ plt .suptitle ('Mixed nrows, ncols' )
524
526
525
527
###############################################################################
526
528
# Similarly,
527
- # :func:`~matplotlib.pyplot.subplot2grid` doesn't work for the same reason:
528
- # each call creates a different parent gridspec .
529
+ # :func:`~matplotlib.pyplot.subplot2grid` works with the same limitation
530
+ # that nrows and ncols cannot change for the layout to look good .
529
531
530
532
fig = plt .figure ()
531
533
@@ -538,23 +540,7 @@ def docomplicated(suptitle=None):
538
540
example_plot (ax2 )
539
541
example_plot (ax3 )
540
542
example_plot (ax4 )
541
-
542
- ###############################################################################
543
- # The way to make this plot compatible with ``constrained_layout`` is again
544
- # to use ``gridspec`` directly
545
-
546
- fig = plt .figure ()
547
- gs = fig .add_gridspec (3 , 3 )
548
-
549
- ax1 = fig .add_subplot (gs [0 , 0 ])
550
- ax2 = fig .add_subplot (gs [0 , 1 :])
551
- ax3 = fig .add_subplot (gs [1 :, 0 :2 ])
552
- ax4 = fig .add_subplot (gs [1 :, - 1 ])
553
-
554
- example_plot (ax1 )
555
- example_plot (ax2 )
556
- example_plot (ax3 )
557
- example_plot (ax4 )
543
+ fig .suptitle ('subplot2grid' )
558
544
559
545
###############################################################################
560
546
# Other Caveats
0 commit comments