|
9 | 9 | Perhaps the primary function used to create figures and axes.
|
10 | 10 | It's also similar to :func:`.matplotlib.pyplot.subplot`,
|
11 | 11 | but creates and places all axes on the figure at once. See also
|
12 |
| - `.matplotlib.Figure.subplots`. |
| 12 | + :meth:`.matplotlib.Figure.subplots`. |
13 | 13 |
|
14 | 14 | :class:`~matplotlib.gridspec.GridSpec`
|
15 | 15 | Specifies the geometry of the grid that a subplot will be
|
|
56 | 56 | # numpy arrays.
|
57 | 57 |
|
58 | 58 | fig2 = plt.figure(constrained_layout=True)
|
59 |
| -spec2 = gridspec.GridSpec(ncols=2, nrows=2) |
| 59 | +spec2 = gridspec.GridSpec(ncols=2, nrows=2, figure=fig2) |
60 | 60 | f2_ax1 = fig2.add_subplot(spec2[0, 0])
|
61 | 61 | f2_ax2 = fig2.add_subplot(spec2[0, 1])
|
62 | 62 | f2_ax3 = fig2.add_subplot(spec2[1, 0])
|
|
68 | 68 | # `Numpy slice <https://docs.scipy.org/doc/numpy/reference/arrays.indexing.html>`_
|
69 | 69 | # syntax for selecing the part of the gridspec each subplot will occupy.
|
70 | 70 | #
|
71 |
| -# Note that we have also used the convenience method `.Figure.add_grisdpec` |
| 71 | +# Note that we have also used the convenience method `.Figure.add_gridspec` |
72 | 72 | # instead of `.gridspec.GridSpec`, potentially saving the user an import,
|
73 | 73 | # and keeping the namespace cleaner.
|
74 | 74 |
|
75 |
| -fig = plt.figure(constrained_layout=True) |
76 |
| -gs = fig.add_gridspec(3, 3) |
77 |
| -ax1 = fig.add_subplot(gs[0, :]) |
78 |
| -ax1.set_title('gs[0, :]') |
79 |
| -ax2 = fig.add_subplot(gs[1, :-1]) |
80 |
| -ax2.set_title('gs[1, :-1]') |
81 |
| -ax3 = fig.add_subplot(gs[1:, -1]) |
82 |
| -ax3.set_title('gs[1:, -1]') |
83 |
| -ax4 = fig.add_subplot(gs[-1, 0]) |
84 |
| -ax4.set_title('gs[-1, 0]') |
85 |
| -ax5 = fig.add_subplot(gs[-1, -2]) |
86 |
| -ax5.set_title('gs[-1, -2]') |
| 75 | +fig3 = plt.figure(constrained_layout=True) |
| 76 | +gs = fig3.add_gridspec(3, 3) |
| 77 | +f3_ax1 = fig3.add_subplot(gs[0, :]) |
| 78 | +f3_ax1.set_title('gs[0, :]') |
| 79 | +f3_ax2 = fig3.add_subplot(gs[1, :-1]) |
| 80 | +f3_ax2.set_title('gs[1, :-1]') |
| 81 | +f3_ax3 = fig3.add_subplot(gs[1:, -1]) |
| 82 | +f3_ax3.set_title('gs[1:, -1]') |
| 83 | +f3_ax4 = fig3.add_subplot(gs[-1, 0]) |
| 84 | +f3_ax4.set_title('gs[-1, 0]') |
| 85 | +f3_ax5 = fig3.add_subplot(gs[-1, -2]) |
| 86 | +f3_ax5.set_title('gs[-1, -2]') |
87 | 87 |
|
88 | 88 | #############################################################################
|
89 | 89 | # :mod:`~matplotlib.gridspec` is also indispensable for creating subplots
|
|
94 | 94 | # and then uses numpy indexing and slices to allocate multiple
|
95 | 95 | # "cells" for a given subplot.
|
96 | 96 |
|
97 |
| -fig3 = plt.figure(constrained_layout=True) |
98 |
| -spec3 = fig3.add_gridspec(ncols=3, nrows=3) |
| 97 | +fig4 = plt.figure(constrained_layout=True) |
| 98 | +spec4 = fig4.add_gridspec(ncols=2, nrows=2) |
99 | 99 | anno_opts = dict(xy=(0.5, 0.5), xycoords='axes fraction',
|
100 | 100 | va='center', ha='center')
|
101 | 101 |
|
102 |
| -ax1 = fig3.add_subplot(spec3[0, 0]) |
103 |
| -ax1.annotate('GridSpec[0, 0]', **anno_opts) |
104 |
| -fig3.add_subplot(spec3[0, 1:]).annotate('GridSpec[0, 1:]', **anno_opts) |
105 |
| -fig3.add_subplot(spec3[1:, 0]).annotate('GridSpec[1:, 0]', **anno_opts) |
106 |
| -fig3.add_subplot(spec3[1:, 1:]).annotate('GridSpec[1:, 1:]', **anno_opts) |
| 102 | +f4_ax1 = fig4.add_subplot(spec4[0, 0]) |
| 103 | +f4_ax1.annotate('GridSpec[0, 0]', **anno_opts) |
| 104 | +fig4.add_subplot(spec4[0, 1]).annotate('GridSpec[0, 1:]', **anno_opts) |
| 105 | +fig4.add_subplot(spec4[1, 0]).annotate('GridSpec[1:, 0]', **anno_opts) |
| 106 | +fig4.add_subplot(spec4[1, 1]).annotate('GridSpec[1:, 1:]', **anno_opts) |
107 | 107 |
|
108 | 108 | ############################################################################
|
109 | 109 | # Another option is to use the ``width_ratios`` and ``height_ratios``
|
|
114 | 114 | # For the sake of demonstration, we'll blindly create the axes within
|
115 | 115 | # ``for`` loops since we won't need them later.
|
116 | 116 |
|
117 |
| -fig4 = plt.figure(constrained_layout=True) |
| 117 | +fig5 = plt.figure(constrained_layout=True) |
118 | 118 | widths = [2, 3, 1.5]
|
119 | 119 | heights = [1, 3, 2]
|
120 |
| -spec4 = fig4.add_gridspec(ncols=3, nrows=3, width_ratios=widths, |
| 120 | +spec5 = fig5.add_gridspec(ncols=3, nrows=3, width_ratios=widths, |
121 | 121 | height_ratios=heights)
|
122 | 122 | for row in range(3):
|
123 | 123 | for col in range(3):
|
124 |
| - ax = fig4.add_subplot(spec4[row, col]) |
| 124 | + ax = fig5.add_subplot(spec5[row, col]) |
125 | 125 | label = 'Width: {}\nHeight: {}'.format(widths[col], heights[row])
|
126 | 126 | ax.annotate(label, (0.1, 0.5), xycoords='axes fraction', va='center')
|
127 | 127 |
|
|
136 | 136 | # gridspec instance.
|
137 | 137 |
|
138 | 138 | gs_kw = dict(width_ratios=widths, height_ratios=heights)
|
139 |
| -fig5, f5_axes = plt.subplots(ncols=3, nrows=3, constrained_layout=True, |
| 139 | +fig6, f6_axes = plt.subplots(ncols=3, nrows=3, constrained_layout=True, |
140 | 140 | gridspec_kw=gs_kw)
|
141 |
| -for r, row in enumerate(f5_axes): |
| 141 | +for r, row in enumerate(f6_axes): |
142 | 142 | for c, ax in enumerate(row):
|
143 | 143 | label = 'Width: {}\nHeight: {}'.format(widths[c], heights[r])
|
144 | 144 | ax.annotate(label, (0.1, 0.5), xycoords='axes fraction', va='center')
|
145 | 145 |
|
146 |
| -fig5.tight_layout() |
147 |
| - |
148 | 146 | ############################################################################
|
149 | 147 | # The ``subplots`` and ``gridspec`` methods can be combined since it is
|
150 | 148 | # sometimes more convenient to make most of the subplots using ``subplots``
|
151 | 149 | # and then remove some and combine them. Here we create a layout with
|
152 | 150 | # the bottom two axes in the last column combined.
|
153 | 151 |
|
154 |
| -fig, axs = plt.subplots(ncols=3, nrows=3) |
155 |
| -gs = axs[1, 2].get_gridspec() |
| 152 | +fig7, f7_axs = plt.subplots(ncols=3, nrows=3) |
| 153 | +gs = f7_axs[1, 2].get_gridspec() |
156 | 154 | # remove the underlying axes
|
157 |
| -for ax in axs[1:, -1]: |
| 155 | +for ax in f7_axs[1:, -1]: |
158 | 156 | ax.remove()
|
159 |
| -axbig = fig.add_subplot(gs[1:, -1]) |
| 157 | +axbig = fig7.add_subplot(gs[1:, -1]) |
160 | 158 | axbig.annotate('Big Axes \nGridSpec[1:, -1]', (0.1, 0.5),
|
161 | 159 | xycoords='axes fraction', va='center')
|
162 | 160 |
|
163 |
| -fig.tight_layout() |
| 161 | +fig7.tight_layout() |
164 | 162 |
|
165 | 163 | ###############################################################################
|
166 | 164 | # Fine Adjustments to a Gridspec Layout
|
|
172 | 170 | # `.Figure.tight_layout` which both adjust subplot sizes to fill the
|
173 | 171 | # figure.
|
174 | 172 |
|
175 |
| -fig6 = plt.figure(constrained_layout=False) |
176 |
| -gs1 = fig6.add_gridspec(nrows=3, ncols=3, left=0.05, right=0.48, wspace=0.05) |
177 |
| -ax1 = fig6.add_subplot(gs1[:-1, :]) |
178 |
| -ax2 = fig6.add_subplot(gs1[-1, :-1]) |
179 |
| -ax3 = fig6.add_subplot(gs1[-1, -1]) |
| 173 | +fig8 = plt.figure(constrained_layout=False) |
| 174 | +gs1 = fig8.add_gridspec(nrows=3, ncols=3, left=0.05, right=0.48, wspace=0.05) |
| 175 | +f8_ax1 = fig8.add_subplot(gs1[:-1, :]) |
| 176 | +f8_ax2 = fig8.add_subplot(gs1[-1, :-1]) |
| 177 | +f8_ax3 = fig8.add_subplot(gs1[-1, -1]) |
180 | 178 |
|
181 | 179 | ###############################################################################
|
182 | 180 | # This is similar to :func:`~matplotlib.pyplot.subplots_adjust`, but it only
|
183 | 181 | # affects the subplots that are created from the given GridSpec.
|
184 | 182 | #
|
185 | 183 | # For example, compare the left and right sides of this figure:
|
186 | 184 |
|
187 |
| -fig7 = plt.figure(constrained_layout=False) |
188 |
| -gs1 = fig7.add_gridspec(nrows=3, ncols=3, left=0.05, right=0.48, |
| 185 | +fig9 = plt.figure(constrained_layout=False) |
| 186 | +gs1 = fig9.add_gridspec(nrows=3, ncols=3, left=0.05, right=0.48, |
189 | 187 | wspace=0.05)
|
190 |
| -ax1 = fig7.add_subplot(gs1[:-1, :]) |
191 |
| -ax2 = fig7.add_subplot(gs1[-1, :-1]) |
192 |
| -ax3 = fig7.add_subplot(gs1[-1, -1]) |
| 188 | +f9_ax1 = fig9.add_subplot(gs1[:-1, :]) |
| 189 | +f9_ax2 = fig9.add_subplot(gs1[-1, :-1]) |
| 190 | +f9_ax3 = fig9.add_subplot(gs1[-1, -1]) |
193 | 191 |
|
194 |
| -gs2 = fig7.add_gridspec(nrows=3, ncols=3, left=0.55, right=0.98, |
| 192 | +gs2 = fig9.add_gridspec(nrows=3, ncols=3, left=0.55, right=0.98, |
195 | 193 | hspace=0.05)
|
196 |
| -ax4 = fig7.add_subplot(gs2[:, :-1]) |
197 |
| -ax5 = fig7.add_subplot(gs2[:-1, -1]) |
198 |
| -ax6 = fig7.add_subplot(gs2[-1, -1]) |
199 |
| - |
| 194 | +f9_ax4 = fig9.add_subplot(gs2[:, :-1]) |
| 195 | +f9_ax5 = fig9.add_subplot(gs2[:-1, -1]) |
| 196 | +f9_ax6 = fig9.add_subplot(gs2[-1, -1]) |
200 | 197 |
|
201 | 198 | ###############################################################################
|
202 | 199 | # GridSpec using SubplotSpec
|
|
209 | 206 | # Note this is also available from the more verbose
|
210 | 207 | # `.gridspec.GridSpecFromSubplotSpec`.
|
211 | 208 |
|
212 |
| -fig = plt.figure(constrained_layout=True) |
213 |
| -gs0 = fig.add_gridspec(1, 2) |
| 209 | +fig10 = plt.figure(constrained_layout=True) |
| 210 | +gs0 = fig10.add_gridspec(1, 2) |
214 | 211 |
|
215 | 212 | gs00 = gs0[0].subgridspec(2, 3)
|
216 | 213 | gs01 = gs0[1].subgridspec(3, 2)
|
217 | 214 |
|
218 | 215 | for a in range(2):
|
219 | 216 | for b in range(3):
|
220 |
| - fig.add_subplot(gs00[a, b]) |
221 |
| - fig.add_subplot(gs01[b, a]) |
| 217 | + fig10.add_subplot(gs00[a, b]) |
| 218 | + fig10.add_subplot(gs01[b, a]) |
222 | 219 |
|
223 | 220 | ###############################################################################
|
224 | 221 | # A Complex Nested GridSpec using SubplotSpec
|
|
235 | 232 | def squiggle_xy(a, b, c, d, i=np.arange(0.0, 2*np.pi, 0.05)):
|
236 | 233 | return np.sin(i*a)*np.cos(i*b), np.sin(i*c)*np.cos(i*d)
|
237 | 234 |
|
238 |
| - |
239 |
| -fig = plt.figure(figsize=(8, 8), constrained_layout=False) |
| 235 | +10 |
| 236 | +fig11 = plt.figure(figsize=(8, 8), constrained_layout=False) |
240 | 237 |
|
241 | 238 | # gridspec inside gridspec
|
242 |
| -outer_grid = fig.add_gridspec(4, 4, wspace=0.0, hspace=0.0) |
| 239 | +outer_grid = fig11.add_gridspec(4, 4, wspace=0.0, hspace=0.0) |
243 | 240 |
|
244 | 241 | for i in range(16):
|
245 | 242 | inner_grid = outer_grid[i].subgridspec(3, 3, wspace=0.0, hspace=0.0)
|
246 | 243 | a, b = int(i/4)+1, i % 4+1
|
247 | 244 | for j, (c, d) in enumerate(product(range(1, 4), repeat=2)):
|
248 |
| - ax = plt.Subplot(fig, inner_grid[j]) |
| 245 | + ax = plt.Subplot(fig11, inner_grid[j]) |
249 | 246 | ax.plot(*squiggle_xy(a, b, c, d))
|
250 | 247 | ax.set_xticks([])
|
251 | 248 | ax.set_yticks([])
|
252 |
| - fig.add_subplot(ax) |
| 249 | + fig11.add_subplot(ax) |
253 | 250 |
|
254 |
| -all_axes = fig.get_axes() |
| 251 | +all_axes = fig11.get_axes() |
255 | 252 |
|
256 | 253 | # show only the outside spines
|
257 | 254 | for ax in all_axes:
|
|
0 commit comments