|
17 | 17 | --------------------------------------- |
18 | 18 |
|
19 | 19 | `~matplotlib.pyplot.subplots` |
20 | | - The primary function used to create figures and a grid of axes. It is |
21 | | - similar to `.pyplot.subplot`, but creates and places all axes on the |
22 | | - figure at once, and returns an object array with handles for |
23 | | - the axes in the grid. See also `.Figure.subplots`. |
| 20 | + The primary function used to create figures and a grid of axes. It |
| 21 | + creates and places all axes on the figure at once, and returns an |
| 22 | + object array with handles for the axes in the grid. See |
| 23 | + `.Figure.subplots`. |
24 | 24 |
|
25 | 25 | or |
26 | 26 |
|
|
70 | 70 | .. redirect-from:: /tutorials/intermediate/gridspec |
71 | 71 |
|
72 | 72 | """ |
73 | | - |
74 | | -import matplotlib.pyplot as plt |
75 | | - |
76 | | -import numpy as np |
77 | | - |
78 | 73 | ############################################################################ |
79 | 74 | # High-level methods for making grids |
80 | 75 | # =================================== |
|
90 | 85 | # use `~.Axes.annotate`, but other examples could be `~.Axes.plot`, |
91 | 86 | # `~.Axes.pcolormesh`, etc. |
92 | 87 |
|
| 88 | +import matplotlib.pyplot as plt |
| 89 | +import numpy as np |
| 90 | + |
93 | 91 | fig, axs = plt.subplots(ncols=2, nrows=2, figsize=(4.5, 3.5), |
94 | 92 | constrained_layout=True) |
| 93 | +# add an artist, in this case a nice label in the middle... |
95 | 94 | for row in range(2): |
96 | 95 | for col in range(2): |
97 | | - axs[row, col].annotate(f'axs[{row}, {col}]', (0.1, 0.5), |
98 | | - xycoords='axes fraction', va='center') |
| 96 | + axs[row, col].annotate(f'axs[{row}, {col}]', (0.5, 0.5), |
| 97 | + transform=axs[row,col].transAxes, |
| 98 | + ha='center', va='center', fontsize=18, |
| 99 | + color='darkgrey') |
99 | 100 | fig.suptitle('plt.subplots()') |
100 | 101 |
|
| 102 | +############################################################################## |
| 103 | +# We will annotate a lot of axes, so lets encapsulate the annotation, rather |
| 104 | +# than having that large piece of annotation code every time we need it: |
| 105 | + |
| 106 | + |
| 107 | +def annotate_axes(ax, text, fontsize=18): |
| 108 | + ax.text(0.5, 0.5, text, transform=ax.transAxes, |
| 109 | + ha="center", va="center", fontsize=fontsize, color="darkgrey") |
| 110 | + |
| 111 | + |
101 | 112 | ############################################################################## |
102 | 113 | # The same effect can be achieved with `~.pyplot.subplot_mosaic`, |
103 | 114 | # but the return type is a dictionary instead of an array, where the user |
|
109 | 120 | ['loleft', 'loright']], |
110 | 121 | figsize=(4.5, 3.5), constrained_layout=True) |
111 | 122 | for k in axd.keys(): |
112 | | - axd[k].annotate(f'axd["{k}"]', (0.1, 0.5), |
113 | | - xycoords='axes fraction', va='center') |
| 123 | + annotate_axes(axd[k], f'axd["{k}"]', fontsize=14) |
114 | 124 | fig.suptitle('plt.subplot_mosaic()') |
115 | 125 |
|
116 | 126 | ############################################################################ |
|
126 | 136 | ['loleft', 'right']], |
127 | 137 | figsize=(4.5, 3.5), constrained_layout=True) |
128 | 138 | for k in axd.keys(): |
129 | | - axd[k].annotate(f'axd["{k}"]', (0.1, 0.5), |
130 | | - xycoords='axes fraction', va='center') |
| 139 | + annotate_axes(axd[k], f'axd["{k}"]', fontsize=14) |
131 | 140 | fig.suptitle('plt.subplot_mosaic()') |
132 | 141 |
|
133 | 142 | ############################################################################ |
|
144 | 153 | # can be passed to `~matplotlib.pyplot.subplots` and |
145 | 154 | # `~matplotlib.pyplot.subplot_mosaic`: |
146 | 155 |
|
147 | | -gs_kw = dict(width_ratios=[1, 2.2], height_ratios=[1, 2]) |
| 156 | +gs_kw = dict(width_ratios=[1.4, 1], height_ratios=[1, 2]) |
148 | 157 | fig, axd = plt.subplot_mosaic([['upleft', 'right'], |
149 | 158 | ['loleft', 'right']], |
150 | 159 | gridspec_kw=gs_kw, figsize=(4.5, 3.5), |
151 | 160 | constrained_layout=True) |
152 | 161 | for k in axd.keys(): |
153 | | - axd[k].annotate(f'axd["{k}"]', (0.1, 0.5), |
154 | | - xycoords='axes fraction', va='center') |
| 162 | + annotate_axes(axd[k], f'axd["{k}"]', fontsize=14) |
155 | 163 | fig.suptitle('plt.subplot_mosaic()') |
156 | 164 |
|
157 | 165 | ############################################################################ |
|
190 | 198 |
|
191 | 199 | fig, axd = plt.subplot_mosaic(outer, constrained_layout=True) |
192 | 200 | for k in axd.keys(): |
193 | | - axd[k].annotate(f'axd["{k}"]', (0.1, 0.5), |
194 | | - xycoords='axes fraction', va='center') |
195 | | -plt.show() |
| 201 | + annotate_axes(axd[k], f'axd["{k}"]') |
196 | 202 |
|
197 | 203 | ############################################################################ |
198 | 204 | # Low-level and advanced grid methods |
|
212 | 218 | fig = plt.figure(figsize=(4.5, 3.5), constrained_layout=True) |
213 | 219 | spec = fig.add_gridspec(ncols=2, nrows=2) |
214 | 220 | ax0 = fig.add_subplot(spec[0, 0]) |
215 | | -ax0.annotate('ax0', (0.1, 0.5), xycoords='axes fraction', va='center') |
| 221 | +annotate_axes(ax0, 'ax0') |
216 | 222 | ax1 = fig.add_subplot(spec[0, 1]) |
217 | | -ax1.annotate('ax1', (0.1, 0.5), xycoords='axes fraction', va='center') |
| 223 | +annotate_axes(ax1, 'ax0') |
218 | 224 | ax2 = fig.add_subplot(spec[1, 0]) |
219 | | -ax2.annotate('ax2', (0.1, 0.5), xycoords='axes fraction', va='center') |
| 225 | +annotate_axes(ax2, 'ax0') |
220 | 226 | ax3 = fig.add_subplot(spec[1, 1]) |
221 | | -ax3.annotate('ax3', (0.1, 0.5), xycoords='axes fraction', va='center') |
| 227 | +annotate_axes(ax3, 'ax0') |
222 | 228 | fig.suptitle('Manually added subplots using add_gridspec') |
223 | 229 |
|
224 | 230 | ############################################################################# |
|
232 | 238 | fig = plt.figure(figsize=(4.5, 3.5), constrained_layout=True) |
233 | 239 | spec = fig.add_gridspec(2, 2) |
234 | 240 | ax0 = fig.add_subplot(spec[0, :]) |
235 | | -ax0.annotate('ax0', (0.1, 0.5), xycoords='axes fraction', va='center') |
| 241 | +annotate_axes(ax0, 'ax0') |
236 | 242 | ax10 = fig.add_subplot(spec[1, 0]) |
237 | | -ax10.annotate('ax10', (0.1, 0.5), xycoords='axes fraction', va='center') |
| 243 | +annotate_axes(ax10, 'ax10') |
238 | 244 | ax11 = fig.add_subplot(spec[1, 1]) |
239 | | -ax11.annotate('ax11', (0.1, 0.5), xycoords='axes fraction', va='center') |
| 245 | +annotate_axes(ax11, 'ax11') |
240 | 246 | fig.suptitle('Manually added subplots, spanning a column') |
241 | 247 |
|
242 | 248 | ############################################################################### |
|
254 | 260 | gs = fig.add_gridspec(nrows=3, ncols=3, left=0.05, right=0.75, |
255 | 261 | hspace=0.1, wspace=0.05) |
256 | 262 | ax0 = fig.add_subplot(gs[:-1, :]) |
257 | | -ax0.annotate('ax0', (0.1, 0.5), xycoords='axes fraction', va='center') |
| 263 | +annotate_axes(ax0, 'ax0') |
258 | 264 | ax1 = fig.add_subplot(gs[-1, :-1]) |
259 | | -ax1.annotate('ax1', (0.1, 0.5), xycoords='axes fraction', va='center') |
| 265 | +annotate_axes(ax1, 'ax1') |
260 | 266 | ax2 = fig.add_subplot(gs[-1, -1]) |
261 | | -ax2.annotate('ax2', (0.1, 0.5), xycoords='axes fraction', va='center') |
| 267 | +annotate_axes(ax2, 'ax2') |
262 | 268 | fig.suptitle('Manual gridspec with right=0.75') |
263 | | -plt.show() |
| 269 | + |
264 | 270 | ############################################################################### |
265 | 271 | # Nested layouts with SubplotSpec |
266 | 272 | # =============================== |
|
281 | 287 | for a in range(2): |
282 | 288 | for b in range(2): |
283 | 289 | ax = fig.add_subplot(gs00[a, b]) |
284 | | - ax.annotate(f'axLeft[{a}, {b}]', (0.1, 0.5), xycoords='axes fraction', |
285 | | - va='center') |
| 290 | + annotate_axes(ax, f'axLeft[{a}, {b}]', fontsize=10) |
286 | 291 | if a == 1 and b == 1: |
287 | 292 | ax.set_xlabel('xlabel') |
288 | 293 | for a in range(3): |
289 | 294 | ax = fig.add_subplot(gs01[a]) |
290 | | - ax.annotate(f'axRight[{a}]', (0.1, 0.5), xycoords='axes fraction', |
291 | | - va='center') |
| 295 | + annotate_axes(ax, f'axRight[{a}, {b}]') |
292 | 296 | if a == 2: |
293 | 297 | ax.set_ylabel('ylabel') |
294 | 298 |
|
|
0 commit comments