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

Skip to content

Commit 1a853e2

Browse files
committed
typos in gridspec tutorial
1 parent 31a8093 commit 1a853e2

File tree

1 file changed

+56
-59
lines changed

1 file changed

+56
-59
lines changed

tutorials/intermediate/gridspec.py

Lines changed: 56 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
Perhaps the primary function used to create figures and axes.
1010
It's also similar to :func:`.matplotlib.pyplot.subplot`,
1111
but creates and places all axes on the figure at once. See also
12-
`.matplotlib.Figure.subplots`.
12+
:meth:`.matplotlib.Figure.subplots`.
1313
1414
:class:`~matplotlib.gridspec.GridSpec`
1515
Specifies the geometry of the grid that a subplot will be
@@ -56,7 +56,7 @@
5656
# numpy arrays.
5757

5858
fig2 = plt.figure(constrained_layout=True)
59-
spec2 = gridspec.GridSpec(ncols=2, nrows=2)
59+
spec2 = gridspec.GridSpec(ncols=2, nrows=2, figure=fig2)
6060
f2_ax1 = fig2.add_subplot(spec2[0, 0])
6161
f2_ax2 = fig2.add_subplot(spec2[0, 1])
6262
f2_ax3 = fig2.add_subplot(spec2[1, 0])
@@ -68,22 +68,22 @@
6868
# `Numpy slice <https://docs.scipy.org/doc/numpy/reference/arrays.indexing.html>`_
6969
# syntax for selecing the part of the gridspec each subplot will occupy.
7070
#
71-
# Note that we have also used the convenience method `.Figure.add_grisdpec`
71+
# Note that we have also used the convenience method `.Figure.add_gridspec`
7272
# instead of `.gridspec.GridSpec`, potentially saving the user an import,
7373
# and keeping the namespace cleaner.
7474

75-
fig = plt.figure(constrained_layout=True)
76-
gs = fig.add_gridspec(3, 3)
77-
ax1 = fig.add_subplot(gs[0, :])
78-
ax1.set_title('gs[0, :]')
79-
ax2 = fig.add_subplot(gs[1, :-1])
80-
ax2.set_title('gs[1, :-1]')
81-
ax3 = fig.add_subplot(gs[1:, -1])
82-
ax3.set_title('gs[1:, -1]')
83-
ax4 = fig.add_subplot(gs[-1, 0])
84-
ax4.set_title('gs[-1, 0]')
85-
ax5 = fig.add_subplot(gs[-1, -2])
86-
ax5.set_title('gs[-1, -2]')
75+
fig3 = plt.figure(constrained_layout=True)
76+
gs = fig3.add_gridspec(3, 3)
77+
f3_ax1 = fig3.add_subplot(gs[0, :])
78+
f3_ax1.set_title('gs[0, :]')
79+
f3_ax2 = fig3.add_subplot(gs[1, :-1])
80+
f3_ax2.set_title('gs[1, :-1]')
81+
f3_ax3 = fig3.add_subplot(gs[1:, -1])
82+
f3_ax3.set_title('gs[1:, -1]')
83+
f3_ax4 = fig3.add_subplot(gs[-1, 0])
84+
f3_ax4.set_title('gs[-1, 0]')
85+
f3_ax5 = fig3.add_subplot(gs[-1, -2])
86+
f3_ax5.set_title('gs[-1, -2]')
8787

8888
#############################################################################
8989
# :mod:`~matplotlib.gridspec` is also indispensable for creating subplots
@@ -94,16 +94,16 @@
9494
# and then uses numpy indexing and slices to allocate multiple
9595
# "cells" for a given subplot.
9696

97-
fig3 = plt.figure(constrained_layout=True)
98-
spec3 = fig3.add_gridspec(ncols=3, nrows=3)
97+
fig4 = plt.figure(constrained_layout=True)
98+
spec4 = fig4.add_gridspec(ncols=2, nrows=2)
9999
anno_opts = dict(xy=(0.5, 0.5), xycoords='axes fraction',
100100
va='center', ha='center')
101101

102-
ax1 = fig3.add_subplot(spec3[0, 0])
103-
ax1.annotate('GridSpec[0, 0]', **anno_opts)
104-
fig3.add_subplot(spec3[0, 1:]).annotate('GridSpec[0, 1:]', **anno_opts)
105-
fig3.add_subplot(spec3[1:, 0]).annotate('GridSpec[1:, 0]', **anno_opts)
106-
fig3.add_subplot(spec3[1:, 1:]).annotate('GridSpec[1:, 1:]', **anno_opts)
102+
f4_ax1 = fig4.add_subplot(spec4[0, 0])
103+
f4_ax1.annotate('GridSpec[0, 0]', **anno_opts)
104+
fig4.add_subplot(spec4[0, 1]).annotate('GridSpec[0, 1:]', **anno_opts)
105+
fig4.add_subplot(spec4[1, 0]).annotate('GridSpec[1:, 0]', **anno_opts)
106+
fig4.add_subplot(spec4[1, 1]).annotate('GridSpec[1:, 1:]', **anno_opts)
107107

108108
############################################################################
109109
# Another option is to use the ``width_ratios`` and ``height_ratios``
@@ -114,14 +114,14 @@
114114
# For the sake of demonstration, we'll blindly create the axes within
115115
# ``for`` loops since we won't need them later.
116116

117-
fig4 = plt.figure(constrained_layout=True)
117+
fig5 = plt.figure(constrained_layout=True)
118118
widths = [2, 3, 1.5]
119119
heights = [1, 3, 2]
120-
spec4 = fig4.add_gridspec(ncols=3, nrows=3, width_ratios=widths,
120+
spec5 = fig5.add_gridspec(ncols=3, nrows=3, width_ratios=widths,
121121
height_ratios=heights)
122122
for row in range(3):
123123
for col in range(3):
124-
ax = fig4.add_subplot(spec4[row, col])
124+
ax = fig5.add_subplot(spec5[row, col])
125125
label = 'Width: {}\nHeight: {}'.format(widths[col], heights[row])
126126
ax.annotate(label, (0.1, 0.5), xycoords='axes fraction', va='center')
127127

@@ -136,31 +136,29 @@
136136
# gridspec instance.
137137

138138
gs_kw = dict(width_ratios=widths, height_ratios=heights)
139-
fig5, f5_axes = plt.subplots(ncols=3, nrows=3, constrained_layout=True,
139+
fig6, f6_axes = plt.subplots(ncols=3, nrows=3, constrained_layout=True,
140140
gridspec_kw=gs_kw)
141-
for r, row in enumerate(f5_axes):
141+
for r, row in enumerate(f6_axes):
142142
for c, ax in enumerate(row):
143143
label = 'Width: {}\nHeight: {}'.format(widths[c], heights[r])
144144
ax.annotate(label, (0.1, 0.5), xycoords='axes fraction', va='center')
145145

146-
fig5.tight_layout()
147-
148146
############################################################################
149147
# The ``subplots`` and ``gridspec`` methods can be combined since it is
150148
# sometimes more convenient to make most of the subplots using ``subplots``
151149
# and then remove some and combine them. Here we create a layout with
152150
# the bottom two axes in the last column combined.
153151

154-
fig, axs = plt.subplots(ncols=3, nrows=3)
155-
gs = axs[1, 2].get_gridspec()
152+
fig7, f7_axs = plt.subplots(ncols=3, nrows=3)
153+
gs = f7_axs[1, 2].get_gridspec()
156154
# remove the underlying axes
157-
for ax in axs[1:, -1]:
155+
for ax in f7_axs[1:, -1]:
158156
ax.remove()
159-
axbig = fig.add_subplot(gs[1:, -1])
157+
axbig = fig7.add_subplot(gs[1:, -1])
160158
axbig.annotate('Big Axes \nGridSpec[1:, -1]', (0.1, 0.5),
161159
xycoords='axes fraction', va='center')
162160

163-
fig.tight_layout()
161+
fig7.tight_layout()
164162

165163
###############################################################################
166164
# Fine Adjustments to a Gridspec Layout
@@ -172,31 +170,30 @@
172170
# `.Figure.tight_layout` which both adjust subplot sizes to fill the
173171
# figure.
174172

175-
fig6 = plt.figure(constrained_layout=False)
176-
gs1 = fig6.add_gridspec(nrows=3, ncols=3, left=0.05, right=0.48, wspace=0.05)
177-
ax1 = fig6.add_subplot(gs1[:-1, :])
178-
ax2 = fig6.add_subplot(gs1[-1, :-1])
179-
ax3 = fig6.add_subplot(gs1[-1, -1])
173+
fig8 = plt.figure(constrained_layout=False)
174+
gs1 = fig8.add_gridspec(nrows=3, ncols=3, left=0.05, right=0.48, wspace=0.05)
175+
f8_ax1 = fig8.add_subplot(gs1[:-1, :])
176+
f8_ax2 = fig8.add_subplot(gs1[-1, :-1])
177+
f8_ax3 = fig8.add_subplot(gs1[-1, -1])
180178

181179
###############################################################################
182180
# This is similar to :func:`~matplotlib.pyplot.subplots_adjust`, but it only
183181
# affects the subplots that are created from the given GridSpec.
184182
#
185183
# For example, compare the left and right sides of this figure:
186184

187-
fig7 = plt.figure(constrained_layout=False)
188-
gs1 = fig7.add_gridspec(nrows=3, ncols=3, left=0.05, right=0.48,
185+
fig9 = plt.figure(constrained_layout=False)
186+
gs1 = fig9.add_gridspec(nrows=3, ncols=3, left=0.05, right=0.48,
189187
wspace=0.05)
190-
ax1 = fig7.add_subplot(gs1[:-1, :])
191-
ax2 = fig7.add_subplot(gs1[-1, :-1])
192-
ax3 = fig7.add_subplot(gs1[-1, -1])
188+
f9_ax1 = fig9.add_subplot(gs1[:-1, :])
189+
f9_ax2 = fig9.add_subplot(gs1[-1, :-1])
190+
f9_ax3 = fig9.add_subplot(gs1[-1, -1])
193191

194-
gs2 = fig7.add_gridspec(nrows=3, ncols=3, left=0.55, right=0.98,
192+
gs2 = fig9.add_gridspec(nrows=3, ncols=3, left=0.55, right=0.98,
195193
hspace=0.05)
196-
ax4 = fig7.add_subplot(gs2[:, :-1])
197-
ax5 = fig7.add_subplot(gs2[:-1, -1])
198-
ax6 = fig7.add_subplot(gs2[-1, -1])
199-
194+
f9_ax4 = fig9.add_subplot(gs2[:, :-1])
195+
f9_ax5 = fig9.add_subplot(gs2[:-1, -1])
196+
f9_ax6 = fig9.add_subplot(gs2[-1, -1])
200197

201198
###############################################################################
202199
# GridSpec using SubplotSpec
@@ -209,16 +206,16 @@
209206
# Note this is also available from the more verbose
210207
# `.gridspec.GridSpecFromSubplotSpec`.
211208

212-
fig = plt.figure(constrained_layout=True)
213-
gs0 = fig.add_gridspec(1, 2)
209+
fig10 = plt.figure(constrained_layout=True)
210+
gs0 = fig10.add_gridspec(1, 2)
214211

215212
gs00 = gs0[0].subgridspec(2, 3)
216213
gs01 = gs0[1].subgridspec(3, 2)
217214

218215
for a in range(2):
219216
for b in range(3):
220-
fig.add_subplot(gs00[a, b])
221-
fig.add_subplot(gs01[b, a])
217+
fig10.add_subplot(gs00[a, b])
218+
fig10.add_subplot(gs01[b, a])
222219

223220
###############################################################################
224221
# A Complex Nested GridSpec using SubplotSpec
@@ -235,23 +232,23 @@
235232
def squiggle_xy(a, b, c, d, i=np.arange(0.0, 2*np.pi, 0.05)):
236233
return np.sin(i*a)*np.cos(i*b), np.sin(i*c)*np.cos(i*d)
237234

238-
239-
fig = plt.figure(figsize=(8, 8), constrained_layout=False)
235+
10
236+
fig11 = plt.figure(figsize=(8, 8), constrained_layout=False)
240237

241238
# gridspec inside gridspec
242-
outer_grid = fig.add_gridspec(4, 4, wspace=0.0, hspace=0.0)
239+
outer_grid = fig11.add_gridspec(4, 4, wspace=0.0, hspace=0.0)
243240

244241
for i in range(16):
245242
inner_grid = outer_grid[i].subgridspec(3, 3, wspace=0.0, hspace=0.0)
246243
a, b = int(i/4)+1, i % 4+1
247244
for j, (c, d) in enumerate(product(range(1, 4), repeat=2)):
248-
ax = plt.Subplot(fig, inner_grid[j])
245+
ax = plt.Subplot(fig11, inner_grid[j])
249246
ax.plot(*squiggle_xy(a, b, c, d))
250247
ax.set_xticks([])
251248
ax.set_yticks([])
252-
fig.add_subplot(ax)
249+
fig11.add_subplot(ax)
253250

254-
all_axes = fig.get_axes()
251+
all_axes = fig11.get_axes()
255252

256253
# show only the outside spines
257254
for ax in all_axes:

0 commit comments

Comments
 (0)