Skip to content

Commit 63054c2

Browse files
committed
DOC: changes on review
1 parent f99590c commit 63054c2

File tree

1 file changed

+73
-37
lines changed

1 file changed

+73
-37
lines changed

tutorials/intermediate/arranging_axes.py

+73-37
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,20 @@
11
"""
2-
==========================
3-
Arranging Axes in a Figure
4-
==========================
2+
===================================
3+
Arranging multiple Axes in a Figure
4+
===================================
55
66
Often more than one axes is wanted on a figure at a time, and usually
77
we want to organize those axes into a regular grid. Matplotlib has a
88
variety of tools for working with grids of axes that have evolved over
99
the history of the library. Here we will discuss the tools we think
10-
users should use most often, and then dicuss some of the older tools,
11-
and the tools that underpin how axes are organized.
10+
users should use most often, the tools that underpin how axes are organized,
11+
and mention some of the older tools.
1212
13-
How to create grid-shaped combinations of axes:
13+
Overview
14+
========
15+
16+
Create grid-shaped combinations of axes
17+
---------------------------------------
1418
1519
`~matplotlib.pyplot.subplots`
1620
The primary function used to create figures and a grid of axes. It is
@@ -21,19 +25,22 @@
2125
or
2226
2327
`~matplotlib.pyplot.subplot_mosaic`
24-
A more flexible function used to create figures and a grid of axes,
25-
with the added flexibility that some of the axes can span rows or
26-
columns, and that the axes are returned in a labelled dictionary instead
27-
of an array. See also `.Figure.subplot_mosaic`.
28+
A simple way to create figures and a grid of axes, with the added
29+
flexibility that axes can also span rows or columns. The axes
30+
are returned in a labelled dictionary instead of an array. See also
31+
`.Figure.subplot_mosaic` and :doc:`/tutorials/provisional/mosaic`.
2832
2933
Sometimes it is natural to have more than one distinct group of axes grids,
3034
in which case Matplotlib has the concept of `~.figure.SubFigure`:
3135
3236
`~matplotlib.figure.SubFigure`
3337
A virtual figure within a figure.
3438
35-
Underlying these is the concept of a `~.gridspec.GridSpec` and
36-
`~.SubplotSpec`:
39+
Underlying tools
40+
----------------
41+
42+
Underlying these are the concept of a `~.gridspec.GridSpec` and
43+
a `~.SubplotSpec`:
3744
3845
`~matplotlib.gridspec.GridSpec`
3946
Specifies the geometry of the grid that a subplot will be
@@ -44,12 +51,21 @@
4451
`~matplotlib.gridspec.SubplotSpec`
4552
Specifies the location of the subplot in the given `.GridSpec`.
4653
47-
An older way to add axes in grids:
54+
Adding single axes at a time
55+
----------------------------
56+
57+
Many tutorials and resources on the web add axes one at a time. We do not
58+
cover that usage here in detail, as it is generally less elegant and flexible,
59+
though sometimes useful for interactive work.
60+
61+
`~matplotlib.pyplot.subplot` or `.Figure.add_subplot`
62+
Adds a single subplot on a figure, with 1-based indexing (inherited from
63+
Matlab). Columns and rows can be spanned by specifying a range of grid
64+
cells.
4865
4966
`~matplotlib.pyplot.subplot2grid`
50-
A helper function that is similar to `.pyplot.subplot`,
51-
but uses 0-based indexing and let subplot to occupy multiple cells.
52-
This function is not covered in this tutorial.
67+
Similar to `.pyplot.subplot`, but uses 0-based indexing and two-d python
68+
slicing to choose cells.
5369
5470
.. redirect-from:: /tutorials/intermediate/gridspec
5571
@@ -80,22 +96,22 @@
8096
for col in range(2):
8197
axs[row, col].annotate(f'axs[{row}, {col}]', (0.1, 0.5),
8298
xycoords='axes fraction', va='center')
83-
fig.suptitle('subplots')
99+
fig.suptitle('plt.subplots()')
84100

85101
##############################################################################
86102
# The same effect can be achieved with `~.pyplot.subplot_mosaic`,
87103
# but the return type is a dictionary instead of an array, where the user
88104
# can give the keys useful meanings. Here we provide two lists, each list
89105
# representing a row, and each element in the list a key representing the
90-
# column. Note that keys can be any dictionary key, but we typically use
91-
# strings:
106+
# column.
92107

93-
fig, axs = plt.subplot_mosaic([['upleft', 'upright'], ['loleft', 'loright']],
108+
fig, axd = plt.subplot_mosaic([['upleft', 'upright'],
109+
['loleft', 'loright']],
94110
figsize=(4.5, 3.5), constrained_layout=True)
95-
for k in axs.keys():
96-
axs[k].annotate(f'axs["{k}"]', (0.1, 0.5),
111+
for k in axd.keys():
112+
axd[k].annotate(f'axd["{k}"]', (0.1, 0.5),
97113
xycoords='axes fraction', va='center')
98-
fig.suptitle('subplot_mosaic')
114+
fig.suptitle('plt.subplot_mosaic()')
99115

100116
############################################################################
101117
# Axes spanning rows or columns in a grid
@@ -106,35 +122,37 @@
106122
# convenient is probably to use `~.pyplot.subplot_mosaic` by repeating one
107123
# of the keys:
108124

109-
fig, axs = plt.subplot_mosaic([['upleft', 'right'], ['loleft', 'right']],
125+
fig, axd = plt.subplot_mosaic([['upleft', 'right'],
126+
['loleft', 'right']],
110127
figsize=(4.5, 3.5), constrained_layout=True)
111-
for k in axs.keys():
112-
axs[k].annotate(f'axs["{k}"]', (0.1, 0.5),
128+
for k in axd.keys():
129+
axd[k].annotate(f'axd["{k}"]', (0.1, 0.5),
113130
xycoords='axes fraction', va='center')
114-
fig.suptitle('subplot_mosaic')
131+
fig.suptitle('plt.subplot_mosaic()')
115132

116133
############################################################################
117134
# See below for the description of how to do the same thing using
118-
# `~matplotlib.gridspec.GridSpec` or ~matplotlib.pyplot.subplot2grid`.
135+
# `~matplotlib.gridspec.GridSpec` or `~matplotlib.pyplot.subplot2grid`.
119136
#
120137
# Variable widths or heights in a grid
121138
# ------------------------------------
122139
#
123140
# Both `~.pyplot.subplots` and `~.pyplot.subplot_mosaic` allow the rows
124141
# in the grid to be different heights, and the columns to be different
125142
# widths using the *gridspec_kw* keyword argument.
126-
# Any parameter accepted by :class:`~matplotlib.gridspec.GridSpec` can
127-
# be passed to `~matplotlib.pyplot.subplots` and
143+
# Spacing parameters accepted by :class:`~matplotlib.gridspec.GridSpec`
144+
# can be passed to `~matplotlib.pyplot.subplots` and
128145
# `~matplotlib.pyplot.subplot_mosaic`:
129146

130147
gs_kw = dict(width_ratios=[1, 2.2], height_ratios=[1, 2])
131-
fig, axs = plt.subplot_mosaic([['upleft', 'right'], ['loleft', 'right']],
148+
fig, axd = plt.subplot_mosaic([['upleft', 'right'],
149+
['loleft', 'right']],
132150
gridspec_kw=gs_kw, figsize=(4.5, 3.5),
133151
constrained_layout=True)
134-
for k in axs.keys():
135-
axs[k].annotate(f'axs["{k}"]', (0.1, 0.5),
152+
for k in axd.keys():
153+
axd[k].annotate(f'axd["{k}"]', (0.1, 0.5),
136154
xycoords='axes fraction', va='center')
137-
fig.suptitle('subplot_mosaic')
155+
fig.suptitle('plt.subplot_mosaic()')
138156

139157
############################################################################
140158
# Nested axes layouts
@@ -158,6 +176,24 @@
158176
subfigs[1].suptitle('subfigs[1]')
159177
subfigs[1].supylabel('ylabel for subfigs[1]')
160178

179+
############################################################################
180+
# It is also possible to nest axes using `~.pyplot.subplot_mosaic` using
181+
# nested lists. This method does not use subfigures, like above, so lacks
182+
# the ability to add per-subfigure ``suptitle`` and ``supxlabel``, etc.
183+
# Rather it is a conveneince wrapper around the `~.SubplotSpec.subgridspec`
184+
# method described below.
185+
186+
inner = [['innerA'],
187+
['innerB']]
188+
outer = [['upleft', inner],
189+
['lowleft', 'lowright']]
190+
191+
fig, axd = plt.subplot_mosaic(outer, constrained_layout=True)
192+
for k in axd.keys():
193+
axd[k].annotate(f'axd["{k}"]', (0.1, 0.5),
194+
xycoords='axes fraction', va='center')
195+
plt.show()
196+
161197
############################################################################
162198
# Low-level and advanced grid methods
163199
# ===================================
@@ -214,7 +250,7 @@
214250
# subplot sizes to fill the figure. Usually such manual placement
215251
# requires iterations to make the axes tick labels not overlap the axes.
216252

217-
fig = plt.figure(constrained_layout=False)
253+
fig = plt.figure(constrained_layout=False, facecolor='0.9')
218254
gs = fig.add_gridspec(nrows=3, ncols=3, left=0.05, right=0.75,
219255
hspace=0.1, wspace=0.05)
220256
ax0 = fig.add_subplot(gs[:-1, :])
@@ -224,9 +260,9 @@
224260
ax2 = fig.add_subplot(gs[-1, -1])
225261
ax2.annotate('ax2', (0.1, 0.5), xycoords='axes fraction', va='center')
226262
fig.suptitle('Manual gridspec with right=0.75')
227-
263+
plt.show()
228264
###############################################################################
229-
# Nested layouts with SubPlotSpec
265+
# Nested layouts with SubplotSpec
230266
# ===============================
231267
#
232268
# You can create nested layout similar to `~.Figure.subfigures` using

0 commit comments

Comments
 (0)