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

Skip to content

Commit 7f7192c

Browse files
committed
DOC: change gridspec tutorial
1 parent 7e986d2 commit 7f7192c

File tree

3 files changed

+49
-41
lines changed

3 files changed

+49
-41
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: 46 additions & 39 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
@@ -40,8 +41,7 @@
4041
# It returns a :class:`~matplotlib.figure.Figure` instance and an array of
4142
# :class:`~matplotlib.axes.Axes` objects.
4243

43-
fig1, f1_axes = plt.subplots(ncols=2, nrows=2)
44-
fig1.tight_layout()
44+
fig1, f1_axes = plt.subplots(ncols=2, nrows=2, contstrained_layout=True)
4545

4646
############################################################################
4747
# For a simple use case such as this, :mod:`~matplotlib.gridspec` is
@@ -53,13 +53,12 @@
5353
# The elements of the gridspec are accessed in generally the same manner as
5454
# numpy arrays.
5555

56-
fig2 = plt.figure()
56+
fig2 = plt.figure(contstrained_layout=True)
5757
spec2 = gridspec.GridSpec(ncols=2, nrows=2)
5858
f2_ax1 = fig2.add_subplot(spec2[0, 0])
5959
f2_ax2 = fig2.add_subplot(spec2[0, 1])
6060
f2_ax3 = fig2.add_subplot(spec2[1, 0])
6161
f2_ax4 = fig2.add_subplot(spec2[1, 1])
62-
fig2.tight_layout()
6362

6463
#############################################################################
6564
# When you want to have subplots of different sizes, however,
@@ -69,17 +68,17 @@
6968
# and then uses typical numpy indexing and slices to allocate multiple
7069
# "cells" for a given subplot.
7170

72-
fig3 = plt.figure()
73-
spec3 = gridspec.GridSpec(ncols=3, nrows=3)
71+
fig3 = plt.figure(contstrained_layout=True)
72+
spec3 = fig3.add_gridspec(ncols=3, nrows=3)
7473
anno_opts = dict(xy=(0.5, 0.5), xycoords='axes fraction',
7574
va='center', ha='center')
7675

7776
fig3.add_subplot(spec3[0, 0]).annotate('GridSpec[0, 0]', **anno_opts)
7877
fig3.add_subplot(spec3[0, 1:]).annotate('GridSpec[0, 1:]', **anno_opts)
7978
fig3.add_subplot(spec3[1:, 0]).annotate('GridSpec[1:, 0]', **anno_opts)
8079
fig3.add_subplot(spec3[1:, 1:]).annotate('GridSpec[1:, 1:]', **anno_opts)
81-
82-
fig3.tight_layout()
80+
fig3.canvas.draw() # Sometime constrained_layout needs an extra draw...
81+
fig3.show()
8382

8483
############################################################################
8584
# Other option is to use the ``width_ratios`` and ``height_ratios``
@@ -90,19 +89,17 @@
9089
# For the sake of demonstration, we'll blindly create the axes within
9190
# ``for`` loops since we won't need them later.
9291

93-
fig4 = plt.figure()
92+
fig4 = plt.figure(contstrained_layout=True)
9493
widths = [2, 3, 1.5]
9594
heights = [1, 3, 2]
96-
spec4 = gridspec.GridSpec(ncols=3, nrows=3, width_ratios=widths,
95+
spec4 = fig4.add_gridspec(ncols=3, nrows=3, width_ratios=widths,
9796
height_ratios=heights)
9897
for row in range(3):
9998
for col in range(3):
10099
ax = fig4.add_subplot(spec4[row, col])
101100
label = 'Width: {}\nHeight: {}'.format(widths[col], heights[row])
102101
ax.annotate(label, (0.1, 0.5), xycoords='axes fraction', va='center')
103102

104-
fig4.tight_layout()
105-
106103
############################################################################
107104
# Learning to use ``width_ratios`` and ``height_ratios`` is particularly
108105
# useful since the top-level function :func:`~matplotlib.pyplot.subplots`
@@ -114,49 +111,47 @@
114111
# gridspec instance.
115112

116113
gs_kw = dict(width_ratios=widths, height_ratios=heights)
117-
fig5, f5_axes = plt.subplots(ncols=3, nrows=3, gridspec_kw=gs_kw)
114+
fig5, f5_axes = plt.subplots(ncols=3, nrows=3, contstrained_layout=True,
115+
gridspec_kw=gs_kw)
118116
for r, row in enumerate(f5_axes):
119117
for c, ax in enumerate(row):
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
# ==========================
@@ -165,19 +160,17 @@
165160
# in which case its layout parameters are set to that of the location of
166161
# the given SubplotSpec.
167162

168-
fig = plt.figure()
169-
gs0 = gridspec.GridSpec(1, 2)
163+
fig = plt.figure(contstrained_layout=True)
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
for a in range(2):
175170
for b in range(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
import numpy as np
190183
from itertools import product
191184

192-
193185
def squiggle_xy(a, b, c, d, i=np.arange(0.0, 2*np.pi, 0.05)):
194186
return np.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
for i in range(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
for j, (c, d) in enumerate(product(range(1, 4), repeat=2)):
206197
ax = plt.Subplot(fig, inner_grid[j])
@@ -225,3 +216,19 @@ def squiggle_xy(a, b, c, d, i=np.arange(0.0, 2*np.pi, 0.05)):
225216
ax.spines['right'].set_visible(True)
226217

227218
plt.show()
219+
220+
#############################################################################
221+
#
222+
# ------------
223+
#
224+
# References
225+
# """"""""""
226+
#
227+
# The usage of the following functions and methods is shown in this example:
228+
229+
matplotlib.pyplot.subplots
230+
matplotlib.figure.Figure.add_gridspec
231+
matplotlib.figure.Figure.add_subplot
232+
matplotlib.gridspec.GridSpec
233+
matplotlib.gridspec.SubplotSpec.subgridspec
234+
matplotlib.gridspec.GridSpecFromSubplotSpec

0 commit comments

Comments
 (0)