8000 DOC: expand gridspec tutorial · matplotlib/matplotlib@54cd35c · GitHub
[go: up one dir, main page]

Skip to content

Commit 54cd35c

Browse files
committed
DOC: expand gridspec tutorial
* add brief quickstart section * remove subplot2grid section * word smithing * added axes to previous examples
1 parent 7e5de9a commit 54cd35c

File tree

1 file changed

+25
-14
lines changed

1 file changed

+25
-14
lines changed

tutorials/intermediate/gridspec.py

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,14 @@
4343
fig1.tight_layout()
4444

4545
############################################################################
46-
# For such a simple use case, :mod:`~matplotlib.gridspec` is perhaps overly
47-
# verbose.
46+
# For a simple use case such as this, :mod:`~matplotlib.gridspec` is
47+
# perhaps overly verbose.
4848
# You have to create the figure and :class:`~matplotlib.gridspec.GridSpec`
49-
# instance separately, then pass the elements gridspec instance to the
49+
# instance separately, then pass elements of gridspec instance to the
5050
# :func:`~matplotlib.figure.Figure.add_subplot` method to create the axes
5151
# objects.
52-
# The elements of the gridspec are accessed as if they where elements of
53-
# a numpy array of the same shape.
52+
# The elements of the gridspec are accessed in generally the same manner as
53+
# numpy arrays.
5454

5555
fig2 = plt.figure()
5656
spec2 = gridspec.GridSpec(ncols=2, nrows=2)
@@ -61,7 +61,7 @@
6161
fig2.tight_layout()
6262

6363
#############################################################################
64-
# When you want to have plots of different sizes, however,
64+
# When you want to have subplots of different sizes, however,
6565
# :mod:`~matplotlib.gridspec` becomes indispensable and provides a couple
6666
# of options.
6767
# The method shown here initializes a uniform grid specification,
@@ -81,15 +81,15 @@
8181
fig3.tight_layout()
8282

8383
############################################################################
84-
# This example shows off using the ``width_ratios`` and ``height_ratios``
85-
# options.
84+
# Other option is to use the ``width_ratios`` and ``height_ratios``
85+
# parameters.
8686
# These keyword arguments are lists of numbers.
8787
# Note that absolute values are meaningless, only their relative ratios
8888
# matter.
8989
# That means that ``width_ratios=[2, 4, 8]`` is equivalent to
9090
# ``width_ratios=[1, 2, 4]`` within equally wide figures.
91-
# We'll blindly create the axes within ``for`` loops since we won't need
92-
# them later.
91+
# For the sake of demonstration, we'll blindly create the axes within
92+
# ``for`` loops since we won't need them later.
9393

9494
fig4 = plt.figure()
9595
widths = [2, 3, 1.5]
@@ -106,8 +106,8 @@
106106

107107
############################################################################
108108
# Learning to use ``width_ratios`` and ``height_ratios`` is particularly
109-
# useful since :func:`~matplotlib.pyplot.subplots` accepts them within the
110-
# ``gridspec_kw`` parameter.
109+
# useful since the top-level function :func:`~matplotlib.pyplot.subplots`
110+
# accepts them within the ``gridspec_kw`` parameter.
111111
# For that matter, any parameter accepted by
112112
# :class:`~matplotlib.gridspec.GridSpec` can be passed to
113113
# :func:`~matplotlib.pyplot.subplots` via the ``gridspec_kw`` parameter.
@@ -133,6 +133,10 @@
133133

134134
fig = plt.figure()
135135
gs1 = gridspec.GridSpec(nrows=3, ncols=3, left=0.05, right=0.48, wspace=0.05)
136+
ax1 = fig.add_subplot(gs1[:-1, :])
137+
ax2 = fig.add_subplot(gs1[-1, :-1])
138+
ax3 = fig.add_subplot(gs1[-1, -1])
139+
136140

137141
###############################################################################
138142
# This is similar to :func:`~matplotlib.pyplot.subplots_adjust`, but it only
@@ -165,8 +169,15 @@
165169
fig = plt.figure()
166170
gs0 = gridspec.GridSpec(1, 2)
167171

168-
gs00 = gridspec.GridSpecFromSubplotSpec(3, 3, subplot_spec=gs0[0])
169-
gs01 = gridspec.GridSpecFromSubplotSpec(3, 3, subplot_spec=gs0[1])
172+
gs00 = gridspec.GridSpecFromSubplotSpec(2, 3, subplot_spec=gs0[0])
173+
gs01 = gridspec.GridSpecFromSubplotSpec(3, 2, subplot_spec=gs0[1])
174+
175+
for a in range(2):
176+
for b in range(3):
177+
fig.add_subplot(gs00[a, b])
178+
fig.add_subplot(gs01[b, a])
179+
180+
fig.tight_layout()
170181

171182
###############################################################################
172183
# A Complex Nested GridSpec using SubplotSpec

0 commit comments

Comments
 (0)
0