|
| 1 | +.. redirect-from:: /tutorials/toolkits/axes_grid |
| 2 | + |
| 3 | +.. _axes_grid1_users-guide-index: |
| 4 | +.. _axes_grid: |
| 5 | + |
| 6 | +====================== |
| 7 | +The axes_grid1 toolkit |
| 8 | +====================== |
| 9 | + |
| 10 | +:mod:`.axes_grid1` provides the following features: |
| 11 | + |
| 12 | +- Helper classes (ImageGrid_, RGBAxes_, AxesDivider_) to ease the layout of |
| 13 | + axes displaying images with a fixed aspect ratio while satisfying additional |
| 14 | + constraints (matching the heights of a colorbar and an image, or fixing the |
| 15 | + padding between images); |
| 16 | +- ParasiteAxes_ (twinx/twiny-like features so that you can plot different data |
| 17 | + (e.g., different y-scale) in a same Axes); |
| 18 | +- AnchoredArtists_ (custom artists which are placed at an anchored position, |
| 19 | + similarly to legends). |
| 20 | + |
| 21 | +.. figure:: /gallery/axes_grid1/images/sphx_glr_demo_axes_grid_001.png |
| 22 | + :target: /gallery/axes_grid1/demo_axes_grid.html |
| 23 | + :align: center |
| 24 | + |
| 25 | +axes_grid1 |
| 26 | +========== |
| 27 | + |
| 28 | +ImageGrid |
| 29 | +--------- |
| 30 | + |
| 31 | +In Matplotlib, axes location and size are usually specified in normalized |
| 32 | +figure coordinates (0 = bottom left, 1 = top right), which makes |
| 33 | +it difficult to achieve a fixed (absolute) padding between images. |
| 34 | +`~.axes_grid1.axes_grid.ImageGrid` can be used to achieve such a padding; see |
| 35 | +its docs for detailed API information. |
| 36 | + |
| 37 | +.. figure:: /gallery/axes_grid1/images/sphx_glr_simple_axesgrid_001.png |
| 38 | + :target: /gallery/axes_grid1/simple_axesgrid.html |
| 39 | + :align: center |
| 40 | + |
| 41 | +* The position of each axes is determined at the drawing time (see |
| 42 | + AxesDivider_), so that the size of the entire grid fits in the |
| 43 | + given rectangle (like the aspect of axes). Note that in this example, |
| 44 | + the paddings between axes are fixed even if you change the figure |
| 45 | + size. |
| 46 | + |
| 47 | +* Axes in the same column share their x-axis, and axes in the same row share |
| 48 | + their y-axis (in the sense of `~.Axes.sharex`, `~.Axes.sharey`). |
| 49 | + Additionally, Axes in the same column all have the same width, and axes in |
| 50 | + the same row all have the same height. These widths and heights are scaled |
| 51 | + in proportion to the axes' view limits (xlim or ylim). |
| 52 | + |
| 53 | + .. figure:: /gallery/axes_grid1/images/sphx_glr_simple_axesgrid2_001.png |
| 54 | + :target: /gallery/axes_grid1/simple_axesgrid2.html |
| 55 | + :align: center |
| 56 | + |
| 57 | +The examples below show what you can do with ImageGrid. |
| 58 | + |
| 59 | +.. figure:: /gallery/axes_grid1/images/sphx_glr_demo_axes_grid_001.png |
| 60 | + :target: /gallery/axes_grid1/demo_axes_grid.html |
| 61 | + :align: center |
| 62 | + |
| 63 | +AxesDivider Class |
| 64 | +----------------- |
| 65 | + |
| 66 | +Behind the scenes, ImageGrid (and RGBAxes, described below) rely on |
| 67 | +`~.axes_grid1.axes_divider.AxesDivider`, whose role is to calculate the |
| 68 | +location of the axes at drawing time. |
| 69 | + |
| 70 | +Users typically do not need to directly instantiate dividers |
| 71 | +by calling `~.axes_grid1.axes_divider.AxesDivider`; instead, |
| 72 | +`~.axes_grid1.axes_divider.make_axes_locatable` can be used to create a divider |
| 73 | +for an Axes:: |
| 74 | + |
| 75 | + ax = subplot(1, 1, 1) |
| 76 | + divider = make_axes_locatable(ax) |
| 77 | + |
| 78 | +`.AxesDivider.append_axes` can then be used to create a new axes on a given |
| 79 | +side ("left", "right", "top", "bottom") of the original axes. |
| 80 | + |
| 81 | +colorbar whose height (or width) is in sync with the main axes |
| 82 | +-------------------------------------------------------------- |
| 83 | + |
| 84 | +.. figure:: /gallery/axes_grid1/images/sphx_glr_simple_colorbar_001.png |
| 85 | + :target: /gallery/axes_grid1/simple_colorbar.html |
| 86 | + :align: center |
| 87 | + |
| 88 | +scatter_hist.py with AxesDivider |
| 89 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 90 | + |
| 91 | +The :doc:`/gallery/lines_bars_and_markers/scatter_hist` example can be |
| 92 | +rewritten using `~.axes_grid1.axes_divider.make_axes_locatable`:: |
| 93 | + |
| 94 | + axScatter = plt.subplot() |
| 95 | + axScatter.scatter(x, y) |
| 96 | + axScatter.set_aspect(1.) |
| 97 | + |
| 98 | + # create new axes on the right and on the top of the current axes. |
| 99 | + divider = make_axes_locatable(axScatter) |
| 100 | + axHistx = divider.append_axes("top", size=1.2, pad=0.1, sharex=axScatter) |
| 101 | + axHisty = divider.append_axes("right", size=1.2, pad=0.1, sharey=axScatter) |
| 102 | + |
| 103 | + # the scatter plot: |
| 104 | + # histograms |
| 105 | + bins = np.arange(-lim, lim + binwidth, binwidth) |
| 106 | + axHistx.hist(x, bins=bins) |
| 107 | + axHisty.hist(y, bins=bins, orientation='horizontal') |
| 108 | + |
| 109 | +See the full source code below. |
| 110 | + |
| 111 | +.. figure:: /gallery/axes_grid1/images/sphx_glr_scatter_hist_locatable_axes_001.png |
| 112 | + :target: /gallery/axes_grid1/scatter_hist_locatable_axes.html |
| 113 | + :align: center |
| 114 | + |
| 115 | +The :doc:`/gallery/axes_grid1/scatter_hist_locatable_axes` using the |
| 116 | +AxesDivider has some advantages over the |
| 117 | +original :doc:`/gallery/lines_bars_and_markers/scatter_hist` in Matplotlib. |
| 118 | +For example, you can set the aspect ratio of the scatter plot, even with the |
| 119 | +x-axis or y-axis is shared accordingly. |
| 120 | + |
| 121 | +ParasiteAxes |
| 122 | +------------ |
| 123 | + |
| 124 | +The ParasiteAxes is an Axes whose location is identical to its host |
| 125 | +axes. The location is adjusted in the drawing time, thus it works even |
| 126 | +if the host change its location (e.g., images). |
| 127 | + |
| 128 | +In most cases, you first create a host axes, which provides a few |
| 129 | +methods that can be used to create parasite axes. They are ``twinx``, |
| 130 | +``twiny`` (which are similar to ``twinx`` and ``twiny`` in the matplotlib) and |
| 131 | +``twin``. ``twin`` takes an arbitrary transformation that maps between the |
| 132 | +data coordinates of the host axes and the parasite axes. The ``draw`` |
| 133 | +method of the parasite axes are never called. Instead, host axes |
| 134 | +collects artists in parasite axes and draws them as if they belong to |
| 135 | +the host axes, i.e., artists in parasite axes are merged to those of |
| 136 | +the host axes and then drawn according to their zorder. The host and |
| 137 | +parasite axes modifies some of the axes behavior. For example, color |
| 138 | +cycle for plot lines are shared between host and parasites. Also, the |
| 139 | +legend command in host, creates a legend that includes lines in the |
| 140 | +parasite axes. To create a host axes, you may use ``host_subplot`` or |
| 141 | +``host_axes`` command. |
| 142 | + |
| 143 | +Example 1: twinx |
| 144 | +~~~~~~~~~~~~~~~~ |
| 145 | + |
| 146 | +.. figure:: /gallery/axes_grid1/images/sphx_glr_parasite_simple_001.png |
| 147 | + :target: /gallery/axes_grid1/parasite_simple.html |
| 148 | + :align: center |
| 149 | + |
| 150 | +Example 2: twin |
| 151 | +~~~~~~~~~~~~~~~ |
| 152 | + |
| 153 | +``twin`` without a transform argument assumes that the parasite axes has the |
| 154 | +same data transform as the host. This can be useful when you want the |
| 155 | +top(or right)-axis to have different tick-locations, tick-labels, or |
| 156 | +tick-formatter for bottom(or left)-axis. :: |
| 157 | + |
| 158 | + ax2 = ax.twin() # now, ax2 is responsible for "top" axis and "right" axis |
| 159 | + ax2.set_xticks([0., .5*np.pi, np.pi, 1.5*np.pi, 2*np.pi], |
| 160 | + labels=["0", r"$\frac{1}{2}\pi$", |
| 161 | + r"$\pi$", r"$\frac{3}{2}\pi$", r"$2\pi$"]) |
| 162 | + |
| 163 | +.. figure:: /gallery/axes_grid1/images/sphx_glr_simple_axisline4_001.png |
| 164 | + :target: /gallery/axes_grid1/simple_axisline4.html |
| 165 | + :align: center |
| 166 | + |
| 167 | +A more sophisticated example using twin. Note that if you change the |
| 168 | +x-limit in the host axes, the x-limit of the parasite axes will change |
| 169 | +accordingly. |
| 170 | + |
| 171 | +.. figure:: /gallery/axes_grid1/images/sphx_glr_parasite_simple2_001.png |
| 172 | + :target: /gallery/axes_grid1/parasite_simple2.html |
| 173 | + :align: center |
| 174 | + |
| 175 | +AnchoredArtists |
| 176 | +--------------- |
| 177 | + |
| 178 | +:mod:`.axes_grid1.anchored_artists` is a collection of artists whose location |
| 179 | +is anchored to the (axes) bbox, similarly to legends. These artists derive |
| 180 | +from `.offsetbox.OffsetBox`, and the artist need to be drawn in canvas |
| 181 | +coordinates. There is limited support for arbitrary transforms. For example, |
| 182 | +the ellipse in the example below will have width and height in data coordinates. |
| 183 | + |
| 184 | +.. figure:: /gallery/axes_grid1/images/sphx_glr_simple_anchored_artists_001.png |
| 185 | + :target: /gallery/axes_grid1/simple_anchored_artists.html |
| 186 | + :align: center |
| 187 | + |
| 188 | +InsetLocator |
| 189 | +------------ |
| 190 | + |
| 191 | +.. seealso:: |
| 192 | + `.Axes.inset_axes` and `.Axes.indicate_inset_zoom` in the main library. |
| 193 | + |
| 194 | +:mod:`.axes_grid1.inset_locator` provides helper classes and functions to |
| 195 | +place inset axes at an anchored position of the parent axes, similarly to |
| 196 | +AnchoredArtist. |
| 197 | + |
| 198 | +`.inset_locator.inset_axes` creates an inset axes whose size is either fixed, |
| 199 | +or a fixed proportion of the parent axes:: |
| 200 | + |
| 201 | + inset_axes = inset_axes(parent_axes, |
| 202 | + width="30%", # width = 30% of parent_bbox |
| 203 | + height=1., # height = 1 inch |
| 204 | + loc='lower left') |
| 205 | + |
| 206 | +creates an inset axes whose width is 30% of the parent axes and whose |
| 207 | +height is fixed at 1 inch. |
| 208 | + |
| 209 | +`.inset_locator.zoomed_inset_axes` creates an inset axes whose data scale is |
| 210 | +that of the parent axes multiplied by some factor, e.g. :: |
| 211 | + |
| 212 | + inset_axes = zoomed_inset_axes(ax, |
| 213 | + 0.5, # zoom = 0.5 |
| 214 | + loc='upper right') |
| 215 | + |
| 216 | +creates an inset axes whose data scale is half of the parent axes. This can be |
| 217 | +useful to mark the zoomed area on the parent axes: |
| 218 | + |
| 219 | +.. figure:: /gallery/axes_grid1/images/sphx_glr_inset_locator_demo_001.png |
| 220 | + :target: /gallery/axes_grid1/inset_locator_demo.html |
| 221 | + :align: center |
| 222 | + |
| 223 | +`.inset_locator.mark_inset` allows marking the location of the area represented |
| 224 | +by the inset axes: |
| 225 | + |
| 226 | +.. figure:: /gallery/axes_grid1/images/sphx_glr_inset_locator_demo2_001.png |
| 227 | + :target: /gallery/axes_grid1/inset_locator_demo2.html |
| 228 | + :align: center |
| 229 | + |
| 230 | +RGBAxes |
| 231 | +------- |
| 232 | + |
| 233 | +RGBAxes is a helper class to conveniently show RGB composite |
| 234 | +images. Like ImageGrid, the location of axes are adjusted so that the |
| 235 | +area occupied by them fits in a given rectangle. Also, the xaxis and |
| 236 | +yaxis of each axes are shared. :: |
| 237 | + |
| 238 | + from mpl_toolkits.axes_grid1.axes_rgb import RGBAxes |
| 239 | + |
| 240 | + fig = plt.figure() |
| 241 | + ax = RGBAxes(fig, [0.1, 0.1, 0.8, 0.8], pad=0.0) |
| 242 | + r, g, b = get_rgb() # r, g, b are 2D images. |
| 243 | + ax.imshow_rgb(r, g, b) |
| 244 | + |
| 245 | +.. figure:: /gallery/axes_grid1/images/sphx_glr_demo_axes_rgb_001.png |
| 246 | + :target: /gallery/axes_grid1/demo_axes_rgb.html |
| 247 | + :align: center |
| 248 | + |
| 249 | +AxesDivider |
| 250 | +=========== |
| 251 | + |
| 252 | +The :mod:`mpl_toolkits.axes_grid1.axes_divider` module provides helper classes |
| 253 | +to adjust the axes positions of a set of images at drawing time. |
| 254 | + |
| 255 | +* :mod:`~mpl_toolkits.axes_grid1.axes_size` provides a class of |
| 256 | + units that are used to determine the size of each axes. For example, |
| 257 | + you can specify a fixed size. |
| 258 | + |
| 259 | +* `~mpl_toolkits.axes_grid1.axes_divider.Divider` is the class that |
| 260 | + calculates the axes position. It divides the given rectangular area into |
| 261 | + several areas. The divider is initialized by setting the lists of horizontal |
| 262 | + and vertical sizes on which the division will be based. Then use |
| 263 | + :meth:`~mpl_toolkits.axes_grid1.axes_divider.Divider.new_locator`, which |
| 264 | + returns a callable object that can be used to set the axes_locator of the |
| 265 | + axes. |
| 266 | + |
| 267 | +Here, we demonstrate how to achieve the following layout: we want to position |
| 268 | +axes in a 3x4 grid (note that `.Divider` makes row indices start from the |
| 269 | +*bottom*\(!) of the grid): |
| 270 | + |
| 271 | +.. code-block:: none |
| 272 | +
|
| 273 | + ┌────────┬────────┬────────┬────────┐ |
| 274 | + │ (2, 0) │ (2, 1) │ (2, 2) │ (2, 3) │ |
| 275 | + ├────────┼────────┼────────┼────────┤ |
| 276 | + │ (1, 0) │ (1, 1) │ (1, 2) │ (1, 3) │ |
| 277 | + ├────────┼────────┼────────┼────────┤ |
| 278 | + │ (0, 0) │ (0, 1) │ (0, 2) │ (0, 3) │ |
| 279 | + └────────┴────────┴────────┴────────┘ |
| 280 | +
|
| 281 | +such that the bottom row has a fixed height of 2 (inches) and the top two rows |
| 282 | +have a height ratio of 2 (middle) to 3 (top). (For example, if the grid has |
| 283 | +a size of 7 inches, the bottom row will be 2 inches, the middle row also 2 |
| 284 | +inches, and the top row 3 inches.) |
| 285 | + |
| 286 | +These constraints are specified using classes from the |
| 287 | +:mod:`~mpl_toolkits.axes_grid1.axes_size` module, namely:: |
| 288 | + |
| 289 | + from mpl_toolkits.axes_grid1.axes_size import Fixed, Scaled |
| 290 | + vert = [Fixed(2), Scaled(2), Scaled(3)] |
| 291 | + |
| 292 | +(More generally, :mod:`~mpl_toolkits.axes_grid1.axes_size` classes define a |
| 293 | +``get_size(renderer)`` method that returns a pair of floats -- a relative size, |
| 294 | +and an absolute size. ``Fixed(2).get_size(renderer)`` returns ``(0, 2)``; |
| 295 | +``Scaled(2).get_size(renderer)`` returns ``(2, 0)``.) |
| 296 | + |
| 297 | +We use these constraints to initialize a `.Divider` object:: |
| 298 | + |
| 299 | + rect = [0.2, 0.2, 0.6, 0.6] # Position of the grid in the figure. |
| 300 | + vert = [Fixed(2), Scaled(2), Scaled(3)] # As above. |
| 301 | + horiz = [...] # Some other horizontal constraints. |
| 302 | + divider = Divider(fig, rect, horiz, vert) |
| 303 | + |
| 304 | +then use `.Divider.new_locator` to create an `.AxesLocator` instance for a |
| 305 | +given grid entry:: |
| 306 | + |
| 307 | + locator = divider.new_locator(nx=0, ny=1) # Grid entry (1, 0). |
| 308 | + |
| 309 | +and make it responsible for locating the axes:: |
| 310 | + |
| 311 | + ax.set_axes_locator(locator) |
| 312 | + |
| 313 | +The `.AxesLocator` is a callable object that returns the location and size of |
| 314 | +the cell at the first column and the second row. |
| 315 | + |
| 316 | +Locators that spans over multiple cells can be created with, e.g.:: |
| 317 | + |
| 318 | + # Columns #0 and #1 ("0-2 range"), row #1. |
| 319 | + locator = divider.new_locator(nx=0, nx1=2, ny=1) |
| 320 | + |
| 321 | +See the example, |
| 322 | + |
| 323 | +.. figure:: /gallery/axes_grid1/images/sphx_glr_simple_axes_divider1_001.png |
| 324 | + :target: /gallery/axes_grid1/simple_axes_divider1.html |
| 325 | + :align: center |
| 326 | + |
| 327 | +You can also adjust the size of each axes according to its x or y |
| 328 | +data limits (AxesX and AxesY). |
| 329 | + |
| 330 | +.. figure:: /gallery/axes_grid1/images/sphx_glr_simple_axes_divider3_001.png |
| 331 | + :target: /gallery/axes_grid1/simple_axes_divider3.html |
| 332 | + :align: center |
0 commit comments