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

Commit8c7700c

Browse files
committed
DOC: change gridspec tutorial
1 parent7e986d2 commit8c7700c

File tree

3 files changed

+28
-36
lines changed

3 files changed

+28
-36
lines changed

‎lib/matplotlib/_constrained_layout.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@ def do_constrained_layout(fig, renderer, h_pad, w_pad,
158158
159159
'''
160160

161+
_log.debug('Starting do_constrained_layout')
161162
invTransFig=fig.transFigure.inverted().transform_bbox
162163

163164
# list of unique gridspecs that contain child axes:

‎lib/matplotlib/figure.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2252,9 +2252,9 @@ def align_labels(self, axs=None):
22522252
22532253
See Also
22542254
--------
2255-
`matplotlib.figure.Figure.align_xlabels`
2255+
matplotlib.figure.Figure.align_xlabels
22562256
2257-
`matplotlib.figure.Figure.align_ylabels`
2257+
matplotlib.figure.Figure.align_ylabels
22582258
"""
22592259
self.align_xlabels(axs=axs)
22602260
self.align_ylabels(axs=axs)

‎tutorials/intermediate/gridspec.py

Lines changed: 25 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@
77
88
:func:`~matplotlib.pyplot.subplots`
99
Perhaps the primary function used to create figures and axes.
10-
It's also similar to :func:`~matplotlib.pyplot.subplot`,
11-
but creates and places all axes on the figure at once.
10+
It's also similar to :func:`.matplotlib.pyplot.subplot`,
11+
but creates and places all axes on the figure at once. See also
12+
`.matplotlib.Figure.subplots`.
1213
1314
:class:`~matplotlib.gridspec.GridSpec`
1415
Specifies the geometry of the grid that a subplot will be
@@ -28,6 +29,7 @@
2829

2930
importmatplotlib.pyplotasplt
3031
importmatplotlib.gridspecasgridspec
32+
plt.rcParams['figure.constrained_layout.use']=True
3133

3234
############################################################################
3335
# Basic Quickstart Guide
@@ -41,7 +43,6 @@
4143
# :class:`~matplotlib.axes.Axes` objects.
4244

4345
fig1,f1_axes=plt.subplots(ncols=2,nrows=2)
44-
fig1.tight_layout()
4546

4647
############################################################################
4748
# For a simple use case such as this, :mod:`~matplotlib.gridspec` is
@@ -54,12 +55,11 @@
5455
# numpy arrays.
5556

5657
fig2=plt.figure()
57-
spec2=gridspec.GridSpec(ncols=2,nrows=2)
58+
spec2=fig2.add_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])
62-
fig2.tight_layout()
6363

6464
#############################################################################
6565
# When you want to have subplots of different sizes, however,
@@ -70,16 +70,16 @@
7070
# "cells" for a given subplot.
7171

7272
fig3=plt.figure()
73-
spec3=gridspec.GridSpec(ncols=3,nrows=3)
73+
spec3=fig3.add_gridspec(ncols=3,nrows=3)
7474
anno_opts=dict(xy=(0.5,0.5),xycoords='axes fraction',
7575
va='center',ha='center')
7676

7777
fig3.add_subplot(spec3[0,0]).annotate('GridSpec[0, 0]',**anno_opts)
7878
fig3.add_subplot(spec3[0,1:]).annotate('GridSpec[0, 1:]',**anno_opts)
7979
fig3.add_subplot(spec3[1:,0]).annotate('GridSpec[1:, 0]',**anno_opts)
8080
fig3.add_subplot(spec3[1:,1:]).annotate('GridSpec[1:, 1:]',**anno_opts)
81-
82-
fig3.tight_layout()
81+
fig3.canvas.draw()# Sometime constrained_layout needs an extra draw...
82+
fig3.show()
8383

8484
############################################################################
8585
# Other option is to use the ``width_ratios`` and ``height_ratios``
@@ -93,16 +93,14 @@
9393
fig4=plt.figure()
9494
widths= [2,3,1.5]
9595
heights= [1,3,2]
96-
spec4=gridspec.GridSpec(ncols=3,nrows=3,width_ratios=widths,
96+
spec4=fig4.add_gridspec(ncols=3,nrows=3,width_ratios=widths,
9797
height_ratios=heights)
9898
forrowinrange(3):
9999
forcolinrange(3):
100100
ax=fig4.add_subplot(spec4[row,col])
101101
label='Width: {}\nHeight: {}'.format(widths[col],heights[row])
102102
ax.annotate(label, (0.1,0.5),xycoords='axes fraction',va='center')
103103

104-
fig4.tight_layout()
105-
106104
############################################################################
107105
# Learning to use ``width_ratios`` and ``height_ratios`` is particularly
108106
# useful since the top-level function :func:`~matplotlib.pyplot.subplots`
@@ -120,43 +118,40 @@
120118
label='Width: {}\nHeight: {}'.format(widths[c],heights[r])
121119
ax.annotate(label, (0.1,0.5),xycoords='axes fraction',va='center')
122120

123-
fig5.tight_layout()
124-
125-
126121
###############################################################################
127122
# Fine Adjustments to a Gridspec Layout
128123
# =====================================
129124
#
130125
# When a GridSpec is explicitly used, you can adjust the layout
131126
# parameters of subplots that are created from the GridSpec.
132127

133-
fig=plt.figure()
134-
gs1=gridspec.GridSpec(nrows=3,ncols=3,left=0.05,right=0.48,wspace=0.05)
135-
ax1=fig.add_subplot(gs1[:-1, :])
136-
ax2=fig.add_subplot(gs1[-1, :-1])
137-
ax3=fig.add_subplot(gs1[-1,-1])
138-
128+
fig6=plt.figure()
129+
gs1=fig6.add_gridspec(nrows=3,ncols=3,left=0.05,right=0.48,wspace=0.05)
130+
ax1=fig6.add_subplot(gs1[:-1, :])
131+
ax2=fig6.add_subplot(gs1[-1, :-1])
132+
ax3=fig6.add_subplot(gs1[-1,-1])
133+
fig6.canvas.draw()
139134

140135
###############################################################################
141136
# This is similar to :func:`~matplotlib.pyplot.subplots_adjust`, but it only
142137
# affects the subplots that are created from the given GridSpec.
143138
#
144139
# For example, compare the left and right sides of this figure:
145140

146-
fig=plt.figure()
147-
gs1=gridspec.GridSpec(nrows=3,ncols=3,left=0.05,right=0.48,
141+
fig=plt.figure(constrained_layout=False)
142+
gs1=fig.add_gridspec(nrows=3,ncols=3,left=0.05,right=0.48,
148143
wspace=0.05)
149144
ax1=fig.add_subplot(gs1[:-1, :])
150145
ax2=fig.add_subplot(gs1[-1, :-1])
151146
ax3=fig.add_subplot(gs1[-1,-1])
152147

153-
154-
gs2=gridspec.GridSpec(nrows=3,ncols=3,left=0.55,right=0.98,
148+
gs2=fig.add_gridspec(nrows=3,ncols=3,left=0.55,right=0.98,
155149
hspace=0.05)
156150
ax4=fig.add_subplot(gs2[:, :-1])
157151
ax5=fig.add_subplot(gs2[:-1,-1])
158152
ax6=fig.add_subplot(gs2[-1,-1])
159153

154+
160155
###############################################################################
161156
# GridSpec using SubplotSpec
162157
# ==========================
@@ -166,18 +161,16 @@
166161
# the given SubplotSpec.
167162

168163
fig=plt.figure()
169-
gs0=gridspec.GridSpec(1,2)
164+
gs0=fig.add_gridspec(1,2)
170165

171-
gs00=gridspec.GridSpecFromSubplotSpec(2,3,subplot_spec=gs0[0])
172-
gs01=gridspec.GridSpecFromSubplotSpec(3,2,subplot_spec=gs0[1])
166+
gs00=gs0[0].subgridspec(2,3)
167+
gs01=gs0[1].subgridspec(3,2)
173168

174169
forainrange(2):
175170
forbinrange(3):
176171
fig.add_subplot(gs00[a,b])
177172
fig.add_subplot(gs01[b,a])
178173

179-
fig.tight_layout()
180-
181174
###############################################################################
182175
# A Complex Nested GridSpec using SubplotSpec
183176
# ===========================================
@@ -189,18 +182,16 @@
189182
importnumpyasnp
190183
fromitertoolsimportproduct
191184

192-
193185
defsquiggle_xy(a,b,c,d,i=np.arange(0.0,2*np.pi,0.05)):
194186
returnnp.sin(i*a)*np.cos(i*b),np.sin(i*c)*np.cos(i*d)
195187

196-
fig=plt.figure(figsize=(8,8))
188+
fig=plt.figure(figsize=(8,8),constrained_layout=False)
197189

198190
# gridspec inside gridspec
199-
outer_grid=gridspec.GridSpec(4,4,wspace=0.0,hspace=0.0)
191+
outer_grid=fig.add_gridspec(4,4,wspace=0.0,hspace=0.0)
200192

201193
foriinrange(16):
202-
inner_grid=gridspec.GridSpecFromSubplotSpec(
203-
3,3,subplot_spec=outer_grid[i],wspace=0.0,hspace=0.0)
194+
inner_grid=outer_grid[i].subgridspec(3,3,wspace=0.0,hspace=0.0)
204195
a,b=int(i/4)+1,i%4+1
205196
forj, (c,d)inenumerate(product(range(1,4),repeat=2)):
206197
ax=plt.Subplot(fig,inner_grid[j])

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp