1
1
"""
2
+ .. redirect-from:: /tutorials/intermediate/gridspec
3
+
4
+ .. _arranging_axes:
5
+
2
6
===================================
3
7
Arranging multiple Axes in a Figure
4
8
===================================
58
62
`~matplotlib.gridspec.SubplotSpec`
59
63
Specifies the location of the subplot in the given `.GridSpec`.
60
64
65
+ .. _fixed_size_axes:
66
+
61
67
Adding single Axes at a time
62
68
----------------------------
63
69
80
86
Similar to `.pyplot.subplot`, but uses 0-based indexing and two-d python
81
87
slicing to choose cells.
82
88
83
- .. redirect-from:: /tutorials/intermediate/gridspec
84
-
85
89
"""
90
+
91
+ # %%
92
+ #
93
+ # As a simple example of manually adding an axes a, lets add a 3 inch x 2 inch
94
+ # Axes to a 4 inch x 3 inch figure. Note that the location of the subplot is
95
+ # defined as [left, bottom, width, height] in figure-normalized units:
96
+
97
+ # sphinx_gallery_thumbnail_number = 2
98
+
99
+ import matplotlib .pyplot as plt
100
+ import numpy as np
101
+
102
+ w , h = 4 , 3
103
+ margin = 0.5
104
+ fig = plt .figure (figsize = (w , h ), facecolor = 'lightblue' )
105
+ ax = fig .add_axes ([margin / w , margin / h , (w - 2 * margin ) / w ,
106
+ (h - 2 * margin ) / h ])
107
+
108
+
86
109
# %%
87
110
# High-level methods for making grids
88
111
# ===================================
97
120
# we use `~.Axes.annotate`, but other examples could be `~.Axes.plot`,
98
121
# `~.Axes.pcolormesh`, etc.
99
122
100
- import matplotlib .pyplot as plt
101
- import numpy as np
102
-
103
123
fig , axs = plt .subplots (ncols = 2 , nrows = 2 , figsize = (5.5 , 3.5 ),
104
124
layout = "constrained" )
105
125
# add an artist, in this case a nice label in the middle...
@@ -146,7 +166,8 @@ def annotate_axes(ax, text, fontsize=18):
146
166
# have a set aspect ratio. This leads to large gaps between Axes by default:
147
167
#
148
168
149
- fig , axs = plt .subplots (2 , 2 , layout = "constrained" , figsize = (5.5 , 3.5 ))
169
+ fig , axs = plt .subplots (2 , 2 , layout = "constrained" ,
170
+ figsize = (5.5 , 3.5 ), facecolor = 'lightblue' )
150
171
for ax in axs .flat :
151
172
ax .set_aspect (1 )
152
173
fig .suptitle ('Fixed aspect Axes' )
@@ -159,7 +180,8 @@ def annotate_axes(ax, text, fontsize=18):
159
180
# provides `~.mpl_toolkits.axes_grid1.axes_grid.ImageGrid` to accomplish
160
181
# a similar effect, but with a non-standard Axes class).
161
182
162
- fig , axs = plt .subplots (2 , 2 , layout = "compressed" , figsize = (5.5 , 3.5 ))
183
+ fig , axs = plt .subplots (2 , 2 , layout = "compressed" , figsize = (5.5 , 3.5 ),
184
+ facecolor = 'lightblue' )
163
185
for ax in axs .flat :
164
186
ax .set_aspect (1 )
165
187
fig .suptitle ('Fixed aspect Axes: compressed' )
@@ -218,7 +240,7 @@ def annotate_axes(ax, text, fontsize=18):
218
240
fig = plt .figure (layout = "constrained" )
219
241
subfigs = fig .subfigures (1 , 2 , wspace = 0.07 , width_ratios = [1.5 , 1. ])
220
242
axs0 = subfigs [0 ].subplots (2 , 2 )
221
- subfigs [0 ].set_facecolor ('0.9 ' )
243
+ subfigs [0 ].set_facecolor ('lightblue ' )
222
244
subfigs [0 ].suptitle ('subfigs[0]\n Left side' )
223
245
subfigs [0 ].supxlabel ('xlabel for subfigs[0]' )
224
246
@@ -315,7 +337,7 @@ def annotate_axes(ax, text, fontsize=18):
315
337
# These spacing parameters can also be passed to `~.pyplot.subplots` and
316
338
# `~.pyplot.subplot_mosaic` as the *gridspec_kw* argument.
317
339
318
- fig = plt .figure (layout = None , facecolor = '0.9 ' )
340
+ fig = plt .figure (layout = None , facecolor = 'lightblue ' )
319
341
gs = fig .add_gridspec (nrows = 3 , ncols = 3 , left = 0.05 , right = 0.75 ,
320
342
hspace = 0.1 , wspace = 0.05 )
321
343
ax0 = fig .add_subplot (gs [:- 1 , :])
0 commit comments