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

Skip to content

Commit 286efc0

Browse files
committed
DOC: changes on review
1 parent f99590c commit 286efc0

File tree

1 file changed

+71
-34
lines changed

1 file changed

+71
-34
lines changed

tutorials/intermediate/arranging_axes.py

Lines changed: 71 additions & 34 deletions
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,7 +96,7 @@
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`,
@@ -90,12 +106,13 @@
90106
# column. Note that keys can be any dictionary key, but we typically use
91107
# strings:
92108

93-
fig, axs = plt.subplot_mosaic([['upleft', 'upright'], ['loleft', 'loright']],
109+
fig, axd = plt.subplot_mosaic([['upleft', 'upright'],
110+
['loleft', 'loright']],
94111
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),
112+
for k in axd.keys():
113+
axd[k].annotate(f'axd["{k}"]', (0.1, 0.5),
97114
xycoords='axes fraction', va='center')
98-
fig.suptitle('subplot_mosaic')
115+
fig.suptitle('plt.subplot_mosaic()')
99116

100117
############################################################################
101118
# Axes spanning rows or columns in a grid
@@ -106,35 +123,37 @@
106123
# convenient is probably to use `~.pyplot.subplot_mosaic` by repeating one
107124
# of the keys:
108125

109-
fig, axs = plt.subplot_mosaic([['upleft', 'right'], ['loleft', 'right']],
126+
fig, axd = plt.subplot_mosaic([['upleft', 'right'],
127+
['loleft', 'right']],
110128
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),
129+
for k in axd.keys():
130+
axd[k].annotate(f'axd["{k}"]', (0.1, 0.5),
113131
xycoords='axes fraction', va='center')
114-
fig.suptitle('subplot_mosaic')
132+
fig.suptitle('plt.subplot_mosaic()')
115133

116134
############################################################################
117135
# See below for the description of how to do the same thing using
118-
# `~matplotlib.gridspec.GridSpec` or ~matplotlib.pyplot.subplot2grid`.
136+
# `~matplotlib.gridspec.GridSpec` or `~matplotlib.pyplot.subplot2grid`.
119137
#
120138
# Variable widths or heights in a grid
121139
# ------------------------------------
122140
#
123141
# Both `~.pyplot.subplots` and `~.pyplot.subplot_mosaic` allow the rows
124142
# in the grid to be different heights, and the columns to be different
125143
# 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
144+
# Spacing parameters accepted by :class:`~matplotlib.gridspec.GridSpec`
145+
# can be passed to `~matplotlib.pyplot.subplots` and
128146
# `~matplotlib.pyplot.subplot_mosaic`:
129147

130148
gs_kw = dict(width_ratios=[1, 2.2], height_ratios=[1, 2])
131-
fig, axs = plt.subplot_mosaic([['upleft', 'right'], ['loleft', 'right']],
149+
fig, axd = plt.subplot_mosaic([['upleft', 'right'],
150+
['loleft', 'right']],
132151
gridspec_kw=gs_kw, figsize=(4.5, 3.5),
133152
constrained_layout=True)
134-
for k in axs.keys():
135-
axs[k].annotate(f'axs["{k}"]', (0.1, 0.5),
153+
for k in axd.keys():
154+
axd[k].annotate(f'axd["{k}"]', (0.1, 0.5),
136155
xycoords='axes fraction', va='center')
137-
fig.suptitle('subplot_mosaic')
156+
fig.suptitle('plt.subplot_mosaic()')
138157

139158
############################################################################
140159
# Nested axes layouts
@@ -158,6 +177,24 @@
158177
subfigs[1].suptitle('subfigs[1]')
159178
subfigs[1].supylabel('ylabel for subfigs[1]')
160179

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

217-
fig = plt.figure(constrained_layout=False)
254+
fig = plt.figure(constrained_layout=False, facecolor=0.9)
218255
gs = fig.add_gridspec(nrows=3, ncols=3, left=0.05, right=0.75,
219256
hspace=0.1, wspace=0.05)
220257
ax0 = fig.add_subplot(gs[:-1, :])
@@ -226,7 +263,7 @@
226263
fig.suptitle('Manual gridspec with right=0.75')
227264

228265
###############################################################################
229-
# Nested layouts with SubPlotSpec
266+
# Nested layouts with SubplotSpec
230267
# ===============================
231268
#
232269
# You can create nested layout similar to `~.Figure.subfigures` using

0 commit comments

Comments
 (0)