@@ -492,40 +492,43 @@ def docomplicated(suptitle=None):
492492# Incompatible functions
493493# ----------------------
494494#
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+
500501
501502fig = plt .figure ()
502503
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+ # third axes that spans both rows in second column:
507+ ax3 = plt .subplot (2 , 2 , (2 , 4 ))
506508
507509example_plot (ax1 )
508510example_plot (ax2 )
509511example_plot (ax3 )
512+ plt .suptitle ('Homogenous nrows, ncols' )
510513
511514###############################################################################
512- # Of course that layout is possible using a gridspec :
515+ # but the following leads to a poor layout :
513516
514517fig = plt .figure ()
515- gs = fig .add_gridspec (2 , 2 )
516518
517- ax1 = fig . add_subplot ( gs [ 0 , 0 ] )
518- ax2 = fig . add_subplot ( gs [ 1 , 0 ] )
519- ax3 = fig . add_subplot ( gs [:, 1 ] )
519+ ax1 = plt . subplot ( 2 , 2 , 1 )
520+ ax2 = plt . subplot ( 2 , 2 , 3 )
521+ ax3 = plt . subplot ( 1 , 2 , 2 )
520522
521523example_plot (ax1 )
522524example_plot (ax2 )
523525example_plot (ax3 )
526+ plt .suptitle ('Mixed nrows, ncols' )
524527
525528###############################################################################
526529# Similarly,
527- # :func:`~matplotlib.pyplot.subplot2grid` doesn't work for the same reason:
528- # each call creates a different parent gridspec .
530+ # :func:`~matplotlib.pyplot.subplot2grid` works with the same limitation
531+ # that nrows and ncols cannot change for the layout to look good .
529532
530533fig = plt .figure ()
531534
@@ -538,23 +541,7 @@ def docomplicated(suptitle=None):
538541example_plot (ax2 )
539542example_plot (ax3 )
540543example_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 )
544+ fig .suptitle ('subplot2grid' )
558545
559546###############################################################################
560547# Other Caveats
0 commit comments