DOC: redo constrained_layout. Revert/edit gridspec · matplotlib/matplotlib@c0f64eb · GitHub
Skip to content

Commit c0f64eb

Browse files
committed
DOC: redo constrained_layout. Revert/edit gridspec
1 parent 8c7700c commit c0f64eb

3 files changed

Lines changed: 69 additions & 40 deletions

File tree

Lines changed: 18 additions & 0 deletions

tutorials/intermediate/constrainedlayout_guide.py

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@
4848
plt.rcParams['savefig.facecolor'] = "0.8"
4949
plt.rcParams['figure.figsize'] = 4.5, 4.
5050

51-
5251
def example_plot(ax, fontsize=12, nodec=False):
5352
ax.plot([1, 2])
5453

@@ -62,7 +61,7 @@ def example_plot(ax, fontsize=12, nodec=False):
6261
ax.set_yticklabels('')
6362

6463

65-
fig, ax = plt.subplots()
64+
fig, ax = plt.subplots(constrained_layout=False)
6665
example_plot(ax, fontsize=24)
6766

6867
###############################################################################
@@ -285,8 +284,10 @@ def example_plot(ax, fontsize=12, nodec=False):
285284
# with :func:`~matplotlib.figure.Figure.subplots` or
286285
# :func:`~matplotlib.gridspec.GridSpec` and
287286
# :func:`~matplotlib.figure.Figure.add_subplot`.
287+
#
288+
# Note that in what follows ``constrained_layout=True``
288289

289-
fig = plt.figure(constrained_layout=True)
290+
fig = plt.figure()
290291

291292
gs1 = gridspec.GridSpec(2, 1, figure=fig)
292293
ax1 = fig.add_subplot(gs1[0])
@@ -296,20 +297,21 @@ def example_plot(ax, fontsize=12, nodec=False):
296297
example_plot(ax2)
297298

298299
###############################################################################
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``
300302

301-
fig = plt.figure(constrained_layout=True)
303+
fig = plt.figure()
302304

303-
gs0 = gridspec.GridSpec(1, 2, figure=fig)
305+
gs0 = fig.add_gridspec(1, 2)
304306

305-
gs1 = gridspec.GridSpecFromSubplotSpec(2, 1, gs0[0])
307+
gs1 = gs0[0].subgridspec(2, 1)
306308
ax1 = fig.add_subplot(gs1[0])
307309
ax2 = fig.add_subplot(gs1[1])
308310

309311
example_plot(ax1)
310312
example_plot(ax2)
311313

312-
gs2 = gridspec.GridSpecFromSubplotSpec(3, 1, gs0[1])
314+
gs2 = gs0[1].subgridspec(3, 1)
313315

314316
for ss in gs2:
315317
ax = fig.add_subplot(ss)
@@ -324,9 +326,9 @@ def example_plot(ax, fontsize=12, nodec=False):
324326
# extent. If we want the top and bottom of the two grids to line up then
325327
# they need to be in the same gridspec:
326328

327-
fig = plt.figure(constrained_layout=True)
329+
fig = plt.figure()
328330

329-
gs0 = gridspec.GridSpec(6, 2, figure=fig)
331+
gs0 = fig.add_gridspec(6, 2)
330332

331333
ax1 = fig.add_subplot(gs0[:3, 0])
332334
ax2 = fig.add_subplot(gs0[3:, 0])
@@ -349,10 +351,10 @@ def example_plot(ax, fontsize=12, nodec=False):
349351

350352

351353
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)
356358

357359
for gs in gsl:
358360
ax = fig.add_subplot(gs)
@@ -381,7 +383,7 @@ def docomplicated(suptitle=None):
381383
# effect on it anymore. (Note that constrained_layout still leaves the space
382384
# for the axes that is moved).
383385

384-
fig, axs = plt.subplots(1, 2, constrained_layout=True)
386+
fig, axs = plt.subplots(1, 2)
385387
example_plot(axs[0], fontsize=12)
386388
axs[1].set_position([0.2, 0.2, 0.4, 0.4])
387389

@@ -395,7 +397,7 @@ def docomplicated(suptitle=None):
395397

396398
from matplotlib.transforms import Bbox
397399

398-
fig, axs = plt.subplots(1, 2, constrained_layout=True)
400+
fig, axs = plt.subplots(1, 2)
399401
example_plot(axs[0], fontsize=12)
400402
fig.execute_constrained_layout()
401403
# put into data-space:
@@ -419,7 +421,7 @@ def docomplicated(suptitle=None):
419421
# to yield a nice layout:
420422

421423

422-
fig = plt.figure(constrained_layout=True)
424+
fig = plt.figure()
423425

424426
ax1 = plt.subplot(221)
425427
ax2 = plt.subplot(223)
@@ -432,8 +434,8 @@ def docomplicated(suptitle=None):
432434
###############################################################################
433435
# Of course that layout is possible using a gridspec:
434436

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)
437439

438440
ax1 = fig.add_subplot(gs[0, 0])
439441
ax2 = fig.add_subplot(gs[1, 0])
@@ -448,7 +450,7 @@ def docomplicated(suptitle=None):
448450
# :func:`~matplotlib.pyplot.subplot2grid` doesn't work for the same reason:
449451
# each call creates a different parent gridspec.
450452

451-
fig = plt.figure(constrained_layout=True)
453+
fig = plt.figure()
452454

453455
ax1 = plt.subplot2grid((3, 3), (0, 0))
454456
ax2 = plt.subplot2grid((3, 3), (0, 1), colspan=2)
@@ -464,8 +466,8 @@ def docomplicated(suptitle=None):
464466
# The way to make this plot compatible with ``constrained_layout`` is again
465467
# to use ``gridspec`` directly
466468

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)
469471

470472
ax1 = fig.add_subplot(gs[0, 0])
471473
ax2 = fig.add_subplot(gs[0, 1:])

tutorials/intermediate/gridspec.py

Lines changed: 27 additions & 18 deletions

0 commit comments

Comments
 (0)