|
1 | 1 | """ |
2 | | -========================== |
3 | | -Arranging Axes in a Figure |
4 | | -========================== |
| 2 | +=================================== |
| 3 | +Arranging multiple Axes in a Figure |
| 4 | +=================================== |
5 | 5 |
|
6 | 6 | Often more than one axes is wanted on a figure at a time, and usually |
7 | 7 | we want to organize those axes into a regular grid. Matplotlib has a |
8 | 8 | variety of tools for working with grids of axes that have evolved over |
9 | 9 | the history of the library. Here we will discuss the tools we think |
10 | | -users should use most often, and then dicuss some of the older tools, |
11 | | -and the tools that underpin how axes are organized. |
| 10 | +users should use most often, the tools that underpin how axes are organized, |
| 11 | +and mention some of the older tools. |
12 | 12 |
|
13 | | -How to create grid-shaped combinations of axes: |
| 13 | +Overview |
| 14 | +======== |
| 15 | +
|
| 16 | +Create grid-shaped combinations of axes |
| 17 | +--------------------------------------- |
14 | 18 |
|
15 | 19 | `~matplotlib.pyplot.subplots` |
16 | 20 | The primary function used to create figures and a grid of axes. It is |
|
21 | 25 | or |
22 | 26 |
|
23 | 27 | `~matplotlib.pyplot.subplot_mosaic` |
24 | | - A more flexible function used to create figures and a grid of axes, |
25 | | - with the added flexibility that some of the axes can span rows or |
26 | | - columns, and that the axes are returned in a labelled dictionary instead |
27 | | - of an array. See also `.Figure.subplot_mosaic`. |
| 28 | + A simple way to create figures and a grid of axes, with the added |
| 29 | + flexibility that axes can also span rows or columns. The axes |
| 30 | + are returned in a labelled dictionary instead of an array. See also |
| 31 | + `.Figure.subplot_mosaic` and :doc:`/tutorials/provisional/mosaic`. |
28 | 32 |
|
29 | 33 | Sometimes it is natural to have more than one distinct group of axes grids, |
30 | 34 | in which case Matplotlib has the concept of `~.figure.SubFigure`: |
31 | 35 |
|
32 | 36 | `~matplotlib.figure.SubFigure` |
33 | 37 | A virtual figure within a figure. |
34 | 38 |
|
35 | | -Underlying these is the concept of a `~.gridspec.GridSpec` and |
36 | | -`~.SubplotSpec`: |
| 39 | +Underlying tools |
| 40 | +---------------- |
| 41 | +
|
| 42 | +Underlying these are the concept of a `~.gridspec.GridSpec` and |
| 43 | +a `~.SubplotSpec`: |
37 | 44 |
|
38 | 45 | `~matplotlib.gridspec.GridSpec` |
39 | 46 | Specifies the geometry of the grid that a subplot will be |
|
44 | 51 | `~matplotlib.gridspec.SubplotSpec` |
45 | 52 | Specifies the location of the subplot in the given `.GridSpec`. |
46 | 53 |
|
47 | | -An older way to add axes in grids: |
| 54 | +Adding single axes at a time |
| 55 | +---------------------------- |
| 56 | +
|
| 57 | +Many tutorials and resources on the web add axes one at a time. We do not |
| 58 | +cover that usage here in detail, as it is generally less elegant and flexible, |
| 59 | +though sometimes useful for interactive work. |
| 60 | +
|
| 61 | +`~matplotlib.pyplot.subplot` or `.Figure.add_subplot` |
| 62 | + Adds a single subplot on a figure, with 1-based indexing (inherited from |
| 63 | + Matlab). Columns and rows can be spanned by specifying a range of grid |
| 64 | + cells. |
48 | 65 |
|
49 | 66 | `~matplotlib.pyplot.subplot2grid` |
50 | | - A helper function that is similar to `.pyplot.subplot`, |
51 | | - but uses 0-based indexing and let subplot to occupy multiple cells. |
52 | | - This function is not covered in this tutorial. |
| 67 | + Similar to `.pyplot.subplot`, but uses 0-based indexing and two-d python |
| 68 | + slicing to choose cells. |
53 | 69 |
|
54 | 70 | .. redirect-from:: /tutorials/intermediate/gridspec |
55 | 71 |
|
|
80 | 96 | for col in range(2): |
81 | 97 | axs[row, col].annotate(f'axs[{row}, {col}]', (0.1, 0.5), |
82 | 98 | xycoords='axes fraction', va='center') |
83 | | -fig.suptitle('subplots') |
| 99 | +fig.suptitle('plt.subplots()') |
84 | 100 |
|
85 | 101 | ############################################################################## |
86 | 102 | # The same effect can be achieved with `~.pyplot.subplot_mosaic`, |
87 | 103 | # but the return type is a dictionary instead of an array, where the user |
88 | 104 | # can give the keys useful meanings. Here we provide two lists, each list |
89 | 105 | # representing a row, and each element in the list a key representing the |
90 | | -# column. Note that keys can be any dictionary key, but we typically use |
91 | | -# strings: |
| 106 | +# column. |
92 | 107 |
|
93 | | -fig, axs = plt.subplot_mosaic([['upleft', 'upright'], ['loleft', 'loright']], |
| 108 | +fig, axd = plt.subplot_mosaic([['upleft', 'upright'], |
| 109 | + ['loleft', 'loright']], |
94 | 110 | figsize=(4.5, 3.5), constrained_layout=True) |
95 | | -for k in axs.keys(): |
96 | | - axs[k].annotate(f'axs["{k}"]', (0.1, 0.5), |
| 111 | +for k in axd.keys(): |
| 112 | + axd[k].annotate(f'axd["{k}"]', (0.1, 0.5), |
97 | 113 | xycoords='axes fraction', va='center') |
98 | | -fig.suptitle('subplot_mosaic') |
| 114 | +fig.suptitle('plt.subplot_mosaic()') |
99 | 115 |
|
100 | 116 | ############################################################################ |
101 | 117 | # Axes spanning rows or columns in a grid |
|
106 | 122 | # convenient is probably to use `~.pyplot.subplot_mosaic` by repeating one |
107 | 123 | # of the keys: |
108 | 124 |
|
109 | | -fig, axs = plt.subplot_mosaic([['upleft', 'right'], ['loleft', 'right']], |
| 125 | +fig, axd = plt.subplot_mosaic([['upleft', 'right'], |
| 126 | + ['loleft', 'right']], |
110 | 127 | figsize=(4.5, 3.5), constrained_layout=True) |
111 | | -for k in axs.keys(): |
112 | | - axs[k].annotate(f'axs["{k}"]', (0.1, 0.5), |
| 128 | +for k in axd.keys(): |
| 129 | + axd[k].annotate(f'axd["{k}"]', (0.1, 0.5), |
113 | 130 | xycoords='axes fraction', va='center') |
114 | | -fig.suptitle('subplot_mosaic') |
| 131 | +fig.suptitle('plt.subplot_mosaic()') |
115 | 132 |
|
116 | 133 | ############################################################################ |
117 | 134 | # See below for the description of how to do the same thing using |
118 | | -# `~matplotlib.gridspec.GridSpec` or ~matplotlib.pyplot.subplot2grid`. |
| 135 | +# `~matplotlib.gridspec.GridSpec` or `~matplotlib.pyplot.subplot2grid`. |
119 | 136 | # |
120 | 137 | # Variable widths or heights in a grid |
121 | 138 | # ------------------------------------ |
122 | 139 | # |
123 | 140 | # Both `~.pyplot.subplots` and `~.pyplot.subplot_mosaic` allow the rows |
124 | 141 | # in the grid to be different heights, and the columns to be different |
125 | 142 | # widths using the *gridspec_kw* keyword argument. |
126 | | -# Any parameter accepted by :class:`~matplotlib.gridspec.GridSpec` can |
127 | | -# be passed to `~matplotlib.pyplot.subplots` and |
| 143 | +# Spacing parameters accepted by :class:`~matplotlib.gridspec.GridSpec` |
| 144 | +# can be passed to `~matplotlib.pyplot.subplots` and |
128 | 145 | # `~matplotlib.pyplot.subplot_mosaic`: |
129 | 146 |
|
130 | 147 | gs_kw = dict(width_ratios=[1, 2.2], height_ratios=[1, 2]) |
131 | | -fig, axs = plt.subplot_mosaic([['upleft', 'right'], ['loleft', 'right']], |
| 148 | +fig, axd = plt.subplot_mosaic([['upleft', 'right'], |
| 149 | + ['loleft', 'right']], |
132 | 150 | gridspec_kw=gs_kw, figsize=(4.5, 3.5), |
133 | 151 | constrained_layout=True) |
134 | | -for k in axs.keys(): |
135 | | - axs[k].annotate(f'axs["{k}"]', (0.1, 0.5), |
| 152 | +for k in axd.keys(): |
| 153 | + axd[k].annotate(f'axd["{k}"]', (0.1, 0.5), |
136 | 154 | xycoords='axes fraction', va='center') |
137 | | -fig.suptitle('subplot_mosaic') |
| 155 | +fig.suptitle('plt.subplot_mosaic()') |
138 | 156 |
|
139 | 157 | ############################################################################ |
140 | 158 | # Nested axes layouts |
|
158 | 176 | subfigs[1].suptitle('subfigs[1]') |
159 | 177 | subfigs[1].supylabel('ylabel for subfigs[1]') |
160 | 178 |
|
| 179 | +############################################################################ |
| 180 | +# It is also possible to nest axes using `~.pyplot.subplot_mosaic` using |
| 181 | +# nested lists. This method does not use subfigures, like above, so lacks |
| 182 | +# the ability to add per-subfigure ``suptitle`` and ``supxlabel``, etc. |
| 183 | +# Rather it is a conveneince wrapper around the `~.SubplotSpec.subgridspec` |
| 184 | +# method described below. |
| 185 | + |
| 186 | +inner = [['innerA'], |
| 187 | + ['innerB']] |
| 188 | +outer = [['upleft', inner], |
| 189 | + ['lowleft', 'lowright']] |
| 190 | + |
| 191 | +fig, axd = plt.subplot_mosaic(outer, constrained_layout=True) |
| 192 | +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() |
| 196 | + |
161 | 197 | ############################################################################ |
162 | 198 | # Low-level and advanced grid methods |
163 | 199 | # =================================== |
|
214 | 250 | # subplot sizes to fill the figure. Usually such manual placement |
215 | 251 | # requires iterations to make the axes tick labels not overlap the axes. |
216 | 252 |
|
217 | | -fig = plt.figure(constrained_layout=False) |
| 253 | +fig = plt.figure(constrained_layout=False, facecolor='0.9') |
218 | 254 | gs = fig.add_gridspec(nrows=3, ncols=3, left=0.05, right=0.75, |
219 | 255 | hspace=0.1, wspace=0.05) |
220 | 256 | ax0 = fig.add_subplot(gs[:-1, :]) |
|
224 | 260 | ax2 = fig.add_subplot(gs[-1, -1]) |
225 | 261 | ax2.annotate('ax2', (0.1, 0.5), xycoords='axes fraction', va='center') |
226 | 262 | fig.suptitle('Manual gridspec with right=0.75') |
227 | | - |
| 263 | +plt.show() |
228 | 264 | ############################################################################### |
229 | | -# Nested layouts with SubPlotSpec |
| 265 | +# Nested layouts with SubplotSpec |
230 | 266 | # =============================== |
231 | 267 | # |
232 | 268 | # You can create nested layout similar to `~.Figure.subfigures` using |
|
0 commit comments