66
66
sometimes useful for interactive work or to place an Axes in a custom
67
67
location:
68
68
69
+ `~matplotlib.figure.Figure.add_axes`
70
+ Adds a single axes at a location specified by
71
+ ``[left, bottom, widtg, height]`` in fractions of figure width or height.
72
+
69
73
`~matplotlib.pyplot.subplot` or `.Figure.add_subplot`
70
74
Adds a single subplot on a figure, with 1-based indexing (inherited from
71
75
Matlab). Columns and rows can be spanned by specifying a range of grid
86
90
# --------------
87
91
#
88
92
# We can create a basic 2-by-2 grid of Axes using
89
- # :func:`~matplotlib.pyplot.subplots`. It returns a
90
- # :class:`~matplotlib.figure.Figure` instance and an array of
91
- # :class:`~matplotlib.axes.Axes` objects. The Axes objects can
92
- # be used to access methods to place artists on the Axes; here we
93
- # use `~.Axes.annotate`, but other examples could be `~.Axes.plot`,
93
+ # `~matplotlib.pyplot.subplots`. It returns a `~matplotlib.figure.Figure`
94
+ # instance and an array of `~matplotlib.axes.Axes` objects. The Axes
95
+ # objects can be used to access methods to place artists on the Axes; here
96
+ # we use `~.Axes.annotate`, but other examples could be `~.Axes.plot`,
94
97
# `~.Axes.pcolormesh`, etc.
95
98
96
99
import matplotlib .pyplot as plt
97
100
import numpy as np
98
101
99
- fig , axs = plt .subplots (ncols = 2 , nrows = 2 , figsize = (4 .5 , 3.5 ),
102
+ fig , axs = plt .subplots (ncols = 2 , nrows = 2 , figsize = (5 .5 , 3.5 ),
100
103
constrained_layout = True )
101
104
# add an artist, in this case a nice label in the middle...
102
105
for row in range (2 ):
@@ -124,9 +127,9 @@ def annotate_axes(ax, text, fontsize=18):
124
127
# representing a row, and each element in the list a key representing the
125
128
# column.
126
129
127
- fig , axd = plt .subplot_mosaic ([['upleft ' , 'upright ' ],
128
- ['loleft ' , 'loright ' ]],
129
- figsize = (4 .5 , 3.5 ), constrained_layout = True )
130
+ fig , axd = plt .subplot_mosaic ([['upper left ' , 'upper right ' ],
131
+ ['lower left ' , 'lower right ' ]],
132
+ figsize = (5 .5 , 3.5 ), constrained_layout = True )
130
133
for k in axd .keys ():
131
134
annotate_axes (axd [k ], f'axd["{ k } "]' , fontsize = 14 )
132
135
fig .suptitle ('plt.subplot_mosaic()' )
@@ -140,9 +143,9 @@ def annotate_axes(ax, text, fontsize=18):
140
143
# convenient is probably to use `~.pyplot.subplot_mosaic` by repeating one
141
144
# of the keys:
142
145
143
- fig , axd = plt .subplot_mosaic ([['upleft ' , 'right' ],
144
- ['loleft ' , 'right' ]],
145
- figsize = (4 .5 , 3.5 ), constrained_layout = True )
146
+ fig , axd = plt .subplot_mosaic ([['upper left ' , 'right' ],
147
+ ['lower left ' , 'right' ]],
148
+ figsize = (5 .5 , 3.5 ), constrained_layout = True )
146
149
for k in axd .keys ():
147
150
annotate_axes (axd [k ], f'axd["{ k } "]' , fontsize = 14 )
148
151
fig .suptitle ('plt.subplot_mosaic()' )
@@ -157,14 +160,14 @@ def annotate_axes(ax, text, fontsize=18):
157
160
# Both `~.pyplot.subplots` and `~.pyplot.subplot_mosaic` allow the rows
158
161
# in the grid to be different heights, and the columns to be different
159
162
# widths using the *gridspec_kw* keyword argument.
160
- # Spacing parameters accepted by :class: `~matplotlib.gridspec.GridSpec`
163
+ # Spacing parameters accepted by `~matplotlib.gridspec.GridSpec`
161
164
# can be passed to `~matplotlib.pyplot.subplots` and
162
165
# `~matplotlib.pyplot.subplot_mosaic`:
163
166
164
167
gs_kw = dict (width_ratios = [1.4 , 1 ], height_ratios = [1 , 2 ])
165
- fig , axd = plt .subplot_mosaic ([['upleft ' , 'right' ],
166
- ['loleft ' , 'right' ]],
167
- gridspec_kw = gs_kw , figsize = (4 .5 , 3.5 ),
168
+ fig , axd = plt .subplot_mosaic ([['upper left ' , 'right' ],
169
+ ['lower left ' , 'right' ]],
170
+ gridspec_kw = gs_kw , figsize = (5 .5 , 3.5 ),
168
171
constrained_layout = True )
169
172
for k in axd .keys ():
170
173
annotate_axes (axd [k ], f'axd["{ k } "]' , fontsize = 14 )
@@ -175,7 +178,7 @@ def annotate_axes(ax, text, fontsize=18):
175
178
# -------------------
176
179
#
177
180
# Sometimes it is helpful to have two or more grids of Axes that
178
- # may not need to be related to one another. The most simple way to
181
+ # may not need to be related to one another. The most simple way toin
179
182
# accomplish this is to use `.Figure.subfigures`. Note that the alignement
180
183
# of the subfigure layouts is independent with the Axes spines in each
181
184
# subfigure having independent positions. See below for a more verbose
@@ -201,8 +204,8 @@ def annotate_axes(ax, text, fontsize=18):
201
204
202
205
inner = [['innerA' ],
203
206
['innerB' ]]
204
- outer = [['upleft ' , inner ],
205
- ['lowleft ' , 'lowright ' ]]
207
+ outer = [['upper left ' , inner ],
208
+ ['lower left ' , 'lower right ' ]]
206
209
207
210
fig , axd = plt .subplot_mosaic (outer , constrained_layout = True )
208
211
for k in axd .keys ():
@@ -227,16 +230,21 @@ def annotate_axes(ax, text, fontsize=18):
227
230
# We can accopmplish a 2x2 grid in the same manner as
228
231
# ``plt.subplots(2, 2)``:
229
232
230
- fig = plt .figure (figsize = (4 .5 , 3.5 ), constrained_layout = True )
233
+ fig = plt .figure (figsize = (5 .5 , 3.5 ), constrained_layout = True )
231
234
spec = fig .add_gridspec (ncols = 2 , nrows = 2 )
235
+
232
236
ax0 = fig .add_subplot (spec [0 , 0 ])
233
237
annotate_axes (ax0 , 'ax0' )
238
+
234
239
ax1 = fig .add_subplot (spec [0 , 1 ])
235
240
annotate_axes (ax1 , 'ax1' )
241
+
236
242
ax2 = fig .add_subplot (spec [1 , 0 ])
237
243
annotate_axes (ax2 , 'ax2' )
244
+
238
245
ax3 = fig .add_subplot (spec [1 , 1 ])
239
246
annotate_axes (ax3 , 'ax3' )
247
+
240
248
fig .suptitle ('Manually added subplots using add_gridspec' )
241
249
242
250
##############################################################################
@@ -248,14 +256,18 @@ def annotate_axes(ax, text, fontsize=18):
248
256
# and the new Axes will span the slice. This would be the same
249
257
# as ``fig, axd = plt.subplot_mosaic([['ax0', 'ax0'], ['ax1', 'ax2']], ...)``:
250
258
251
- fig = plt .figure (figsize = (4 .5 , 3.5 ), constrained_layout = True )
259
+ fig = plt .figure (figsize = (5 .5 , 3.5 ), constrained_layout = True )
252
260
spec = fig .add_gridspec (2 , 2 )
261
+
253
262
ax0 = fig .add_subplot (spec [0 , :])
254
263
annotate_axes (ax0 , 'ax0' )
264
+
255
265
ax10 = fig .add_subplot (spec [1 , 0 ])
256
266
annotate_axes (ax10 , 'ax10' )
267
+
257
268
ax11 = fig .add_subplot (spec [1 , 1 ])
258
269
annotate_axes (ax11 , 'ax11' )
270
+
259
271
fig .suptitle ('Manually added subplots, spanning a column' )
260
272
261
273
###############################################################################
@@ -288,7 +300,7 @@ def annotate_axes(ax, text, fontsize=18):
288
300
# -------------------------------
289
301
#
290
302
# You can create nested layout similar to `~.Figure.subfigures` using
291
- # `~.gridspec.SubplotSpec.subgridspec`. Here the Axes spines _are_
303
+ # `~.gridspec.SubplotSpec.subgridspec`. Here the Axes spines *are*
292
304
# aligned.
293
305
#
294
306
# Note this is also available from the more verbose
@@ -316,7 +328,7 @@ def annotate_axes(ax, text, fontsize=18):
316
328
317
329
###############################################################################
318
330
# Here's a more sophisticated example of nested *GridSpec*: We create an outer
319
- # 4x4 grid with each cell containing and inner 3x3 grid of Axes. We outline
331
+ # 4x4 grid with each cell containing an inner 3x3 grid of Axes. We outline
320
332
# the outer 4x4 grid by hiding appropriate spines in each of the inner 3x3
321
333
# grids.
322
334
@@ -352,7 +364,7 @@ def squiggle_xy(a, b, c, d, i=np.arange(0.0, 2*np.pi, 0.05)):
352
364
# ============
353
365
#
354
366
# - More details about :doc:`subplot mosaic </tutorials/provisional/mosaic>`.
355
- # - more details about
367
+ # - More details about
356
368
# :doc:`constrained layout
357
369
# </tutorials/intermediate/constrainedlayout_guide>`, used to align
358
370
# spacing in most of these examples.
0 commit comments