1
1
"""
2
- ===================================
3
- Arranging multiple Axes in a Figure
4
- ===================================
2
+ =====================================
3
+ Arranging multiple * Axes* in a Figure
4
+ =====================================
5
5
6
- Often more than one " Axes" is wanted on a figure at a time, usually
6
+ Often more than one * Axes* is wanted on a figure at a time, usually
7
7
organized into a regular grid. Matplotlib has a variety of tools for
8
- working with grids of Axes that have evolved over the history of the library.
8
+ working with grids of * Axes* that have evolved over the history of the library.
9
9
Here we will discuss the tools we think users should use most often, the tools
10
- that underpin how Axes are organized, and mention some of the older tools.
10
+ that underpin how * Axes* are organized, and mention some of the older tools.
11
11
12
12
.. note::
13
13
14
- We use "Axes" to refer to the object that contains plotting
15
- artists and the x- and y-Axis. See `<figure_parts>`_ for more details of
16
- Matplotlib terminology.
14
+ Matplotlib uses *Axes* to refer to an *Artist* that contains plotting
15
+ *Artists* and the x- and y-*Axis*. Another term that is often used is
16
+ "subplot", which refers to an *Axes* that is arranged in a grid.
17
+ See `<figure_parts>`_ for more details of Matplotlib terminology.
17
18
18
19
Overview
19
20
========
20
21
21
- Create grid-shaped combinations of Axes
22
- ---------------------------------------
22
+ Create grid-shaped combinations of * Axes*
23
+ -----------------------------------------
23
24
24
25
`~matplotlib.pyplot.subplots`
25
- The primary function used to create figures and a grid of Axes. It
26
- creates and places all Axes on the figure at once, and returns an
27
- object array with handles for the Axes in the grid. See
26
+ The primary function used to create figures and a grid of * Axes* . It
27
+ creates and places all * Axes* on the figure at once, and returns an
28
+ object array with handles for the * Axes* in the grid. See
28
29
`.Figure.subplots`.
29
30
30
31
or
31
32
32
33
`~matplotlib.pyplot.subplot_mosaic`
33
- A simple way to create figures and a grid of Axes, with the added
34
- flexibility that Axes can also span rows or columns. The Axes
34
+ A simple way to create figures and a grid of * Axes* , with the added
35
+ flexibility that * Axes* can also span rows or columns. The * Axes*
35
36
are returned in a labelled dictionary instead of an array. See also
36
37
`.Figure.subplot_mosaic` and :doc:`/tutorials/provisional/mosaic`.
37
38
38
- Sometimes it is natural to have more than one distinct group of Axes grids,
39
+ Sometimes it is natural to have more than one distinct group of * Axes* grids,
39
40
in which case Matplotlib has the concept of `~.figure.SubFigure`:
40
41
41
42
`~matplotlib.figure.SubFigure`
56
57
`~matplotlib.gridspec.SubplotSpec`
57
58
Specifies the location of the subplot in the given `.GridSpec`.
58
59
59
- Adding single Axes at a time
60
- ----------------------------
60
+ Adding single * Axes* at a time
61
+ ------------------------------
61
62
62
- The above functions create all Axes in a single function call. It is also
63
- possible to add Axes one at a time, and this was originally how Matplotlib
63
+ The above functions create all * Axes* in a single function call. It is also
64
+ possible to add * Axes* one at a time, and this was originally how Matplotlib
64
65
used to work. Doing so is generally less elegant and flexible, though
65
- sometimes useful for interactive work or to place an Axes in a custom location:
66
+ sometimes useful for interactive work or to place an * Axes* in a custom location:
66
67
67
68
`~matplotlib.pyplot.subplot` or `.Figure.add_subplot`
68
69
Adds a single subplot on a figure, with 1-based indexing (inherited from
83
84
# Basic 2x2 grid
84
85
# --------------
85
86
#
86
- # We can create a basic 2-by-2 grid of Axes using
87
+ # We can create a basic 2-by-2 grid of * Axes* using
87
88
# :func:`~matplotlib.pyplot.subplots`. It returns a
88
89
# :class:`~matplotlib.figure.Figure` instance and an array of
89
- # :class:`~matplotlib.axes.Axes` objects. The Axes objects can
90
- # be used to access methods to place artists on the Axes; here we
90
+ # :class:`~matplotlib.axes.Axes` objects. The * Axes* objects can
91
+ # be used to access methods to place artists on the * Axes* ; here we
91
92
# use `~.Axes.annotate`, but other examples could be `~.Axes.plot`,
92
93
# `~.Axes.pcolormesh`, etc.
93
94
106
107
fig .suptitle ('plt.subplots()' )
107
108
108
109
##############################################################################
109
- # We will annotate a lot of Axes, so lets encapsulate the annotation, rather
110
+ # We will annotate a lot of * Axes* , so lets encapsulate the annotation, rather
110
111
# than having that large piece of annotation code every time we need it:
111
112
112
113
@@ -130,10 +131,10 @@ def annotate_axes(ax, text, fontsize=18):
130
131
fig .suptitle ('plt.subplot_mosaic()' )
131
132
132
133
############################################################################
133
- # Axes spanning rows or columns in a grid
134
- # ---------------------------------------
134
+ # * Axes* spanning rows or columns in a grid
135
+ # -----------------------------------------
135
136
#
136
- # Sometimes we want Axes to span rows or columns of the grid.
137
+ # Sometimes we want * Axes* to span rows or columns of the grid.
137
138
# There are actually multiple ways to accomplish this, but the most
138
139
# convenient is probably to use `~.pyplot.subplot_mosaic` by repeating one
139
140
# of the keys:
@@ -169,13 +170,13 @@ def annotate_axes(ax, text, fontsize=18):
169
170
fig .suptitle ('plt.subplot_mosaic()' )
170
171
171
172
############################################################################
172
- # Nested Axes layouts
173
- # -------------------
173
+ # Nested * Axes* layouts
174
+ # ---------------------
174
175
#
175
- # Sometimes it is helpful to have two or more grids of Axes that
176
+ # Sometimes it is helpful to have two or more grids of * Axes* that
176
177
# may not need to be related to one another. The most simple way to
177
178
# accomplish this is to use `.Figure.subfigures`. Note that the alignement
178
- # of the subfigure layouts is independent with the Axes spines in each
179
+ # of the subfigure layouts is independent with the * Axes* spines in each
179
180
# subfigure having independent positions. See below for a more verbose
180
181
# way to acheive the same effect with `~.gridspec.GridSpecFromSubplotSpec`.
181
182
@@ -191,7 +192,7 @@ def annotate_axes(ax, text, fontsize=18):
191
192
subfigs [1 ].supylabel ('ylabel for subfigs[1]' )
192
193
193
194
############################################################################
194
- # It is also possible to nest Axes using `~.pyplot.subplot_mosaic` using
195
+ # It is also possible to nest * Axes* using `~.pyplot.subplot_mosaic` using
195
196
# nested lists. This method does not use subfigures, like above, so lacks
196
197
# the ability to add per-subfigure ``suptitle`` and ``supxlabel``, etc.
197
198
# Rather it is a conveneince wrapper around the `~.SubplotSpec.subgridspec`
@@ -210,14 +211,14 @@ def annotate_axes(ax, text, fontsize=18):
210
211
# Low-level and advanced grid methods
211
212
# ===================================
212
213
#
213
- # Internally, the arrangement of a grid of Axes is controlled by creating
214
- # instances of `~.GridSpec` and `~.SubplotSpec`. GridSpec defines a
215
- # (possibly non-uniform) grid of cells. Indexing into the GridSpec returns
214
+ # Internally, the arrangement of a grid of * Axes* is controlled by creating
215
+ # instances of `~.GridSpec` and `~.SubplotSpec`. * GridSpec* defines a
216
+ # (possibly non-uniform) grid of cells. Indexing into the * GridSpec* returns
216
217
# a SubplotSpec that covers one or more grid cells, and can be used to
217
- # specify the location of an Axes or "subplot" .
218
+ # specify the location of an * Axes* .
218
219
#
219
- # The following examples show how to use low-level methods to arrange Axes
220
- # using GridSpec objects.
220
+ # The following examples show how to use low-level methods to arrange * Axes*
221
+ # using * GridSpec* objects.
221
222
#
222
223
# Basic 2x2 grid
223
224
# --------------
@@ -238,12 +239,12 @@ def annotate_axes(ax, text, fontsize=18):
238
239
fig .suptitle ('Manually added subplots using add_gridspec' )
239
240
240
241
##############################################################################
241
- # Axes spanning rows or grids in a grid
242
- # -------------------------------------
242
+ # * Axes* spanning rows or grids in a grid
243
+ # ---------------------------------------
243
244
#
244
245
# We can index the *spec* array using `NumPy slice syntax
245
246
# <https://numpy.org/doc/stable/reference/arrays.indexing.html>`_
246
- # and the new Axes will span the slice. This would be the same
247
+ # and the new * Axes* will span the slice. This would be the same
247
248
# as ``fig, axd = plt.subplot_mosaic([['ax0', 'ax0'], ['ax1', 'ax2']], ...)``:
248
249
249
250
fig = plt .figure (figsize = (4.5 , 3.5 ), constrained_layout = True )
@@ -257,15 +258,15 @@ def annotate_axes(ax, text, fontsize=18):
257
258
fig .suptitle ('Manually added subplots, spanning a column' )
258
259
259
260
###############################################################################
260
- # Manual adjustments to a GridSpec layout
261
- # ---------------------------------------
261
+ # Manual adjustments to a * GridSpec* layout
262
+ # -----------------------------------------
262
263
#
263
- # When a GridSpec is explicitly used, you can adjust the layout
264
- # parameters of subplots that are created from the GridSpec. Note this
264
+ # When a * GridSpec* is explicitly used, you can adjust the layout
265
+ # parameters of subplots that are created from the * GridSpec* . Note this
265
266
# option is not compatible with ``constrained_layout`` or
266
267
# `.Figure.tight_layout` which both ignore *left* and *right* and adjust
267
268
# subplot sizes to fill the figure. Usually such manual placement
268
- # requires iterations to make the Axes tick labels not overlap the Axes.
269
+ # requires iterations to make the * Axes* tick labels not overlap the * Axes* .
269
270
#
270
271
# These spacing parameters can also be passed to `~.pyplot.subplots` and
271
272
# `~.pyplot.subplot_mosaic` as the *gridspec_kw* argument.
@@ -286,7 +287,7 @@ def annotate_axes(ax, text, fontsize=18):
286
287
# -------------------------------
287
288
#
288
289
# You can create nested layout similar to `~.Figure.subfigures` using
289
- # `~.gridspec.SubplotSpec.subgridspec`. Here the Axes spines _are_
290
+ # `~.gridspec.SubplotSpec.subgridspec`. Here the * Axes* spines _are_
290
291
# aligned.
291
292
#
292
293
# Note this is also available from the more verbose
@@ -313,9 +314,10 @@ def annotate_axes(ax, text, fontsize=18):
313
314
fig .suptitle ('nested gridspecs' )
314
315
315
316
###############################################################################
316
- # Here's a more sophisticated example of nested GridSpec: We create an outer
317
- # 4x4 grid with each cell containing and inner 3x3 grid of Axes. We outline the
318
- # outer 4x4 grid by hiding appropriate spines in each of the inner 3x3 grids.
317
+ # Here's a more sophisticated example of nested *GridSpec*: We create an outer
318
+ # 4x4 grid with each cell containing and inner 3x3 grid of *Axes*. We outline
319
+ # the outer 4x4 grid by hiding appropriate spines in each of the inner 3x3
320
+ # grids.
319
321
320
322
321
323
def squiggle_xy (a , b , c , d , i = np .arange (0.0 , 2 * np .pi , 0.05 )):
0 commit comments