Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit7f4d162

Browse files
committed
DOC: redo constrained_layout. Revert/edit gridspec
1 parent8c7700c commit7f4d162

File tree

2 files changed

+51
-40
lines changed

2 files changed

+51
-40
lines changed

‎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
defexample_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
forssings2:
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
defdocomplicated(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
forgsingsl:
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
frommatplotlib.transformsimportBbox
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
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
# Basic Quickstart Guide
3636
# ======================
3737
#
38-
# These first two examples show how to create a basic4-by-4 grid using
38+
# These first two examples show how to create a basic2-by-2 grid using
3939
# both :func:`~matplotlib.pyplot.subplots` and :mod:`~matplotlib.gridspec`.
4040
#
4141
# Using :func:`~matplotlib.pyplot.subplots` is quite simple.
@@ -55,7 +55,7 @@
5555
# numpy arrays.
5656

5757
fig2=plt.figure()
58-
spec2=fig2.add_gridspec(ncols=2,nrows=2)
58+
spec2=gridspec.GridSpec(ncols=2,nrows=2)
5959
f2_ax1=fig2.add_subplot(spec2[0,0])
6060
f2_ax2=fig2.add_subplot(spec2[0,1])
6161
f2_ax3=fig2.add_subplot(spec2[1,0])
@@ -68,21 +68,25 @@
6868
# The method shown here initializes a uniform grid specification,
6969
# and then uses typical numpy indexing and slices to allocate multiple
7070
# "cells" for a given subplot.
71+
#
72+
# Note that we have also used the convenience method `.Figure.add_grisdpec`
73+
# instead of `.gridspec.GridSpec`, potentially saving the user an import,
74+
# and keeping the namespace cleaner.
7175

7276
fig3=plt.figure()
7377
spec3=fig3.add_gridspec(ncols=3,nrows=3)
7478
anno_opts=dict(xy=(0.5,0.5),xycoords='axes fraction',
7579
va='center',ha='center')
7680

77-
fig3.add_subplot(spec3[0,0]).annotate('GridSpec[0, 0]',**anno_opts)
81+
ax1=fig3.add_subplot(spec3[0,0])
82+
ax1.annotate('GridSpec[0, 0]',**anno_opts)
83+
ax1.set_ylabel('Ylab')
7884
fig3.add_subplot(spec3[0,1:]).annotate('GridSpec[0, 1:]',**anno_opts)
7985
fig3.add_subplot(spec3[1:,0]).annotate('GridSpec[1:, 0]',**anno_opts)
8086
fig3.add_subplot(spec3[1:,1:]).annotate('GridSpec[1:, 1:]',**anno_opts)
81-
fig3.canvas.draw()# Sometime constrained_layout needs an extra draw...
82-
fig3.show()
8387

8488
############################################################################
85-
#Other option is to use the ``width_ratios`` and ``height_ratios``
89+
#Another option is to use the ``width_ratios`` and ``height_ratios``
8690
# parameters. These keyword arguments are lists of numbers.
8791
# Note that absolute values are meaningless, only their relative ratios
8892
# matter. That means that ``width_ratios=[2, 4, 8]`` is equivalent to
@@ -123,33 +127,35 @@
123127
# =====================================
124128
#
125129
# When a GridSpec is explicitly used, you can adjust the layout
126-
# parameters of subplots that are created from the GridSpec.
130+
# parameters of subplots that are created from the GridSpec. Note this
131+
# option is not compatible with ``constrained_layout`` or
132+
# `.Figure.tight_layout` which both adjust subplot sizes to fill the
133+
# figure.
127134

128-
fig6=plt.figure()
135+
fig6=plt.figure(constrained_layout=False)
129136
gs1=fig6.add_gridspec(nrows=3,ncols=3,left=0.05,right=0.48,wspace=0.05)
130137
ax1=fig6.add_subplot(gs1[:-1, :])
131138
ax2=fig6.add_subplot(gs1[-1, :-1])
132139
ax3=fig6.add_subplot(gs1[-1,-1])
133-
fig6.canvas.draw()
134140

135141
###############################################################################
136142
# This is similar to :func:`~matplotlib.pyplot.subplots_adjust`, but it only
137143
# affects the subplots that are created from the given GridSpec.
138144
#
139145
# For example, compare the left and right sides of this figure:
140146

141-
fig=plt.figure(constrained_layout=False)
142-
gs1=fig.add_gridspec(nrows=3,ncols=3,left=0.05,right=0.48,
147+
fig7=plt.figure(constrained_layout=False)
148+
gs1=fig7.add_gridspec(nrows=3,ncols=3,left=0.05,right=0.48,
143149
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])
150+
ax1=fig7.add_subplot(gs1[:-1, :])
151+
ax2=fig7.add_subplot(gs1[-1, :-1])
152+
ax3=fig7.add_subplot(gs1[-1,-1])
147153

148-
gs2=fig.add_gridspec(nrows=3,ncols=3,left=0.55,right=0.98,
154+
gs2=fig7.add_gridspec(nrows=3,ncols=3,left=0.55,right=0.98,
149155
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])
156+
ax4=fig7.add_subplot(gs2[:, :-1])
157+
ax5=fig7.add_subplot(gs2[:-1,-1])
158+
ax6=fig7.add_subplot(gs2[-1,-1])
153159

154160

155161
###############################################################################
@@ -159,6 +165,9 @@
159165
# You can create GridSpec from the :class:`~matplotlib.gridspec.SubplotSpec`,
160166
# in which case its layout parameters are set to that of the location of
161167
# the given SubplotSpec.
168+
#
169+
# Note this is also available from the more verbose
170+
# `.gridspec.GridSpecFromSubplotSpec`.
162171

163172
fig=plt.figure()
164173
gs0=fig.add_gridspec(1,2)

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp