Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit dca12a4

Browse files
committed
DOC: redo constrained_layout. Revert/edit gridspec
1 parent 7f7192c commit dca12a4

File tree

3 files changed

+99
-50
lines changed

3 files changed

+99
-50
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
New convenience methods for GridSpec
2+
------------------------------------
3+
4+
There are new convenience methods for `.gridspec.GridSpec` and
5+
`.gridspec.GridSpecFromSubplotSpec`. Instead of the former we can
6+
now call `.Figure.add_gridspec` and for the latter `.SubplotSpec.subgridspec`.
7+
8+
.. code-block:: python
9+
10+
import matplotlib.pyplot as plt
11+
12+
fig = plt.figure()
13+
gs0 = fig.add_gridspec(3, 1)
14+
ax1 = fig.add_subplot(gs0[0])
15+
ax2 = fig.add_subplot(gs0[1])
16+
gssub = gs0[2].subgridspec(1, 3)
17+
for i in range(3):
18+
fig.add_subplot(gssub[0, i])

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: 57 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -27,21 +27,22 @@
2727
2828
"""
2929

30+
import matplotlib
3031
import matplotlib.pyplot as plt
3132
import matplotlib.gridspec as gridspec
3233

3334
############################################################################
3435
# Basic Quickstart Guide
3536
# ======================
3637
#
37-
# These first two examples show how to create a basic 4-by-4 grid using
38+
# These first two examples show how to create a basic 2-by-2 grid using
3839
# both :func:`~matplotlib.pyplot.subplots` and :mod:`~matplotlib.gridspec`.
3940
#
4041
# Using :func:`~matplotlib.pyplot.subplots` is quite simple.
4142
# It returns a :class:`~matplotlib.figure.Figure` instance and an array of
4243
# :class:`~matplotlib.axes.Axes` objects.
4344

44-
fig1, f1_axes = plt.subplots(ncols=2, nrows=2, contstrained_layout=True)
45+
fig1, f1_axes = plt.subplots(ncols=2, nrows=2, constrained_layout=True)
4546

4647
############################################################################
4748
# For a simple use case such as this, :mod:`~matplotlib.gridspec` is
@@ -53,43 +54,66 @@
5354
# The elements of the gridspec are accessed in generally the same manner as
5455
# numpy arrays.
5556

56-
fig2 = plt.figure(contstrained_layout=True)
57+
fig2 = plt.figure(constrained_layout=True)
5758
spec2 = gridspec.GridSpec(ncols=2, nrows=2)
5859
f2_ax1 = fig2.add_subplot(spec2[0, 0])
5960
f2_ax2 = fig2.add_subplot(spec2[0, 1])
6061
f2_ax3 = fig2.add_subplot(spec2[1, 0])
6162
f2_ax4 = fig2.add_subplot(spec2[1, 1])
6263

6364
#############################################################################
64-
# When you want to have subplots of different sizes, however,
65-
# :mod:`~matplotlib.gridspec` becomes indispensable and provides a couple
66-
# of options.
67-
# The method shown here initializes a uniform grid specification,
68-
# and then uses typical numpy indexing and slices to allocate multiple
65+
# The power of gridspec comes in being able to create subplots that span
66+
# rows and columns. Note the
67+
# `Numpy slice <https://docs.scipy.org/doc/numpy/reference/arrays.indexing.html>`_
68+
# syntax for selecing the part of the gridspec each subplot will occupy.
69+
#
70+
# Note that we have also used the convenience method `.Figure.add_grisdpec`
71+
# instead of `.gridspec.GridSpec`, potentially saving the user an import,
72+
# and keeping the namespace cleaner.
73+
74+
fig = plt.figure(constrained_layout=True)
75+
gs = fig.add_gridspec(3, 3)
76+
ax1 = fig.add_subplot(gs[0, :])
77+
ax1.set_title('gs[0, :]')
78+
ax2 = fig.add_subplot(gs[1, :-1])
79+
ax2.set_title('gs[1, :-1]')
80+
ax3 = fig.add_subplot(gs[1:, -1])
81+
ax3.set_title('gs[1:, -1]')
82+
ax4 = fig.add_subplot(gs[-1, 0])
83+
ax4.set_title('gs[-1, 0]')
84+
ax5 = fig.add_subplot(gs[-1, -2])
85+
ax5.set_title('gs[-1, -2]')
86+
87+
#############################################################################
88+
# :mod:`~matplotlib.gridspec` is also indispensable for creating subplots
89+
# of different widths via a couple of methods.
90+
#
91+
# The method shown here is similar to the one above and initializes a
92+
# uniform grid specification,
93+
# and then uses numpy indexing and slices to allocate multiple
6994
# "cells" for a given subplot.
7095

71-
fig3 = plt.figure(contstrained_layout=True)
96+
fig3 = plt.figure(constrained_layout=True)
7297
spec3 = fig3.add_gridspec(ncols=3, nrows=3)
7398
anno_opts = dict(xy=(0.5, 0.5), xycoords='axes fraction',
7499
va='center', ha='center')
75100

76-
fig3.add_subplot(spec3[0, 0]).annotate('GridSpec[0, 0]', **anno_opts)
101+
ax1 = fig3.add_subplot(spec3[0, 0])
102+
ax1.annotate('GridSpec[0, 0]', **anno_opts)
77103
fig3.add_subplot(spec3[0, 1:]).annotate('GridSpec[0, 1:]', **anno_opts)
78104
fig3.add_subplot(spec3[1:, 0]).annotate('GridSpec[1:, 0]', **anno_opts)
79105
fig3.add_subplot(spec3[1:, 1:]).annotate('GridSpec[1:, 1:]', **anno_opts)
80-
fig3.canvas.draw() # Sometime constrained_layout needs an extra draw...
81-
fig3.show()
82106

83107
############################################################################
84-
# Other option is to use the ``width_ratios`` and ``height_ratios``
108+
# Another option is to use the ``width_ratios`` and ``height_ratios``
85109
# parameters. These keyword arguments are lists of numbers.
86110
# Note that absolute values are meaningless, only their relative ratios
87111
# matter. That means that ``width_ratios=[2, 4, 8]`` is equivalent to
88112
# ``width_ratios=[1, 2, 4]`` within equally wide figures.
89113
# For the sake of demonstration, we'll blindly create the axes within
90114
# ``for`` loops since we won't need them later.
91115

92-
fig4 = plt.figure(contstrained_layout=True)
116+
fig4 = plt.figure(constrained_layout=True)
93117
widths = [2, 3, 1.5]
94118
heights = [1, 3, 2]
95119
spec4 = fig4.add_gridspec(ncols=3, nrows=3, width_ratios=widths,
@@ -111,7 +135,7 @@
111135
# gridspec instance.
112136

113137
gs_kw = dict(width_ratios=widths, height_ratios=heights)
114-
fig5, f5_axes = plt.subplots(ncols=3, nrows=3, contstrained_layout=True,
138+
fig5, f5_axes = plt.subplots(ncols=3, nrows=3, constrained_layout=True,
115139
gridspec_kw=gs_kw)
116140
for r, row in enumerate(f5_axes):
117141
for c, ax in enumerate(row):
@@ -123,33 +147,35 @@
123147
# =====================================
124148
#
125149
# When a GridSpec is explicitly used, you can adjust the layout
126-
# parameters of subplots that are created from the GridSpec.
150+
# parameters of subplots that are created from the GridSpec. Note this
151+
# option is not compatible with ``constrained_layout`` or
152+
# `.Figure.tight_layout` which both adjust subplot sizes to fill the
153+
# figure.
127154

128-
fig6 = plt.figure()
155+
fig6 = plt.figure(constrained_layout=False)
129156
gs1 = fig6.add_gridspec(nrows=3, ncols=3, left=0.05, right=0.48, wspace=0.05)
130157
ax1 = fig6.add_subplot(gs1[:-1, :])
131158
ax2 = fig6.add_subplot(gs1[-1, :-1])
132159
ax3 = fig6.add_subplot(gs1[-1, -1])
133-
fig6.canvas.draw()
134160

135161
###############################################################################
136162
# This is similar to :func:`~matplotlib.pyplot.subplots_adjust`, but it only
137163
# affects the subplots that are created from the given GridSpec.
138164
#
139165
# For example, compare the left and right sides of this figure:
140166

141-
fig = plt.figure(constrained_layout=False)
142-
gs1 = fig.add_gridspec(nrows=3, ncols=3, left=0.05, right=0.48,
167+
fig7 = plt.figure(constrained_layout=False)
168+
gs1 = fig7.add_gridspec(nrows=3, ncols=3, left=0.05, right=0.48,
143169
wspace=0.05)
144-
ax1 = fig.add_subplot(gs1[:-1, :])
145-
ax2 = fig.add_subplot(gs1[-1, :-1])
146-
ax3 = fig.add_subplot(gs1[-1, -1])
170+
ax1 = fig7.add_subplot(gs1[:-1, :])
171+
ax2 = fig7.add_subplot(gs1[-1, :-1])
172+
ax3 = fig7.add_subplot(gs1[-1, -1])
147173

148-
gs2 = fig.add_gridspec(nrows=3, ncols=3, left=0.55, right=0.98,
174+
gs2 = fig7.add_gridspec(nrows=3, ncols=3, left=0.55, right=0.98,
149175
hspace=0.05)
150-
ax4 = fig.add_subplot(gs2[:, :-1])
151-
ax5 = fig.add_subplot(gs2[:-1, -1])
152-
ax6 = fig.add_subplot(gs2[-1, -1])
176+
ax4 = fig7.add_subplot(gs2[:, :-1])
177+
ax5 = fig7.add_subplot(gs2[:-1, -1])
178+
ax6 = fig7.add_subplot(gs2[-1, -1])
153179

154180

155181
###############################################################################
@@ -159,8 +185,11 @@
159185
# You can create GridSpec from the :class:`~matplotlib.gridspec.SubplotSpec`,
160186
# in which case its layout parameters are set to that of the location of
161187
# the given SubplotSpec.
188+
#
189+
# Note this is also available from the more verbose
190+
# `.gridspec.GridSpecFromSubplotSpec`.
162191

163-
fig = plt.figure(contstrained_layout=True)
192+
fig = plt.figure(constrained_layout=True)
164193
gs0 = fig.add_gridspec(1, 2)
165194

166195
gs00 = gs0[0].subgridspec(2, 3)

0 commit comments

Comments
 (0)