{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"%matplotlib inline"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n# Overview of axes_grid1 toolkit\n\n\nControlling the layout of plots with the\n:mod:`mpl_toolkits.axes_grid1` toolkit.\n\n\n\nWhat is axes_grid1 toolkit?\n===========================\n\n:mod:`mpl_toolkits.axes_grid1` is a collection of helper classes to ease\ndisplaying (multiple) images with matplotlib. In matplotlib, the axes location\n(and size) is specified in the normalized figure coordinates, which\nmay not be ideal for displaying images that needs to have a given\naspect ratio. For example, it helps if you have a colorbar whose\nheight always matches that of the image. `ImageGrid`_, `RGB Axes`_ and\n`AxesDivider`_ are helper classes that deal with adjusting the\nlocation of (multiple) Axes. They provides a framework to adjust the\nposition of multiple axes at the drawing time. `ParasiteAxes`_\nprovides twinx(or twiny)-like features so that you can plot different\ndata (e.g., different y-scale) in a same Axes. `AnchoredArtists`_\nincludes custom artists which are placed at some anchored position,\nlike the legend.\n\n.. figure:: ../../gallery/axes_grid1/images/sphx_glr_demo_axes_grid_001.png\n :target: ../../gallery/axes_grid1/demo_axes_grid.html\n :align: center\n :scale: 50\n\n Demo Axes Grid\n\n\naxes_grid1\n==========\n\nImageGrid\n---------\n\nA grid of Axes.\n\nIn Matplotlib, the axes location (and size) is specified in normalized\nfigure coordinates. This may not be ideal for images that needs to be\ndisplayed with a given aspect ratio; for example, it is difficult to\ndisplay multiple images of a same size with some fixed padding between\nthem. `~.axes_grid1.axes_grid.ImageGrid` can be used in such a case; see its\ndocs for a detailed list of the parameters it accepts.\n\n.. figure:: ../../gallery/axes_grid1/images/sphx_glr_simple_axesgrid_001.png\n :target: ../../gallery/axes_grid1/simple_axesgrid.html\n :align: center\n :scale: 50\n\n Simple Axesgrid\n\n* The position of each axes is determined at the drawing time (see\n `AxesDivider`_), so that the size of the entire grid fits in the\n given rectangle (like the aspect of axes). Note that in this example,\n the paddings between axes are fixed even if you changes the figure\n size.\n\n* axes in the same column has a same axes width (in figure\n coordinate), and similarly, axes in the same row has a same\n height. The widths (height) of the axes in the same row (column) are\n scaled according to their view limits (xlim or ylim).\n\n .. figure:: ../../gallery/axes_grid1/images/sphx_glr_simple_axesgrid2_001.png\n :target: ../../gallery/axes_grid1/simple_axesgrid2.html\n :align: center\n :scale: 50\n\n Simple Axes Grid\n\n* xaxis are shared among axes in a same column. Similarly, yaxis are\n shared among axes in a same row. Therefore, changing axis properties\n (view limits, tick location, etc. either by plot commands or using\n your mouse in interactive backends) of one axes will affect all\n other shared axes.\n\nThe examples below show what you can do with ImageGrid.\n\n.. figure:: ../../gallery/axes_grid1/images/sphx_glr_demo_axes_grid_001.png\n :target: ../../gallery/axes_grid1/demo_axes_grid.html\n :align: center\n :scale: 50\n\n Demo Axes Grid\n\n\nAxesDivider Class\n-----------------\n\nBehind the scene, the ImageGrid class and the RGBAxes class utilize the\n`~.axes_grid1.axes_divider.AxesDivider` class, whose role is to calculate the\nlocation of the axes at drawing time. Direct use of the\nAxesDivider class will not be necessary for most users. The\naxes_divider module provides a helper function\n`~.axes_grid1.axes_divider.make_axes_locatable`, which can be useful.\nIt takes a existing axes instance and create a divider for it. ::\n\n ax = subplot(1, 1, 1)\n divider = make_axes_locatable(ax)\n\n*make_axes_locatable* returns an instance of the\n`~.axes_grid1.axes_divider.AxesDivider` class. It provides an\n`~.AxesDivider.append_axes` method that\ncreates a new axes on the given side of (\"top\", \"right\", \"bottom\" and\n\"left\") of the original axes.\n\n\ncolorbar whose height (or width) in sync with the master axes\n-------------------------------------------------------------\n\n.. figure:: ../../gallery/axes_grid1/images/sphx_glr_simple_colorbar_001.png\n :target: ../../gallery/axes_grid1/simple_colorbar.html\n :align: center\n :scale: 50\n\n Simple Colorbar\n\n\nscatter_hist.py with AxesDivider\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n\nThe :doc:`/gallery/lines_bars_and_markers/scatter_hist` example can be\nrewritten using `~.axes_grid1.axes_divider.make_axes_locatable`::\n\n axScatter = subplot(111)\n axScatter.scatter(x, y)\n axScatter.set_aspect(1.)\n\n # create new axes on the right and on the top of the current axes.\n divider = make_axes_locatable(axScatter)\n axHistx = divider.append_axes(\"top\", size=1.2, pad=0.1, sharex=axScatter)\n axHisty = divider.append_axes(\"right\", size=1.2, pad=0.1, sharey=axScatter)\n\n # the scatter plot:\n # histograms\n bins = np.arange(-lim, lim + binwidth, binwidth)\n axHistx.hist(x, bins=bins)\n axHisty.hist(y, bins=bins, orientation='horizontal')\n\nSee the full source code below.\n\n.. figure:: ../../gallery/axes_grid1/images/sphx_glr_scatter_hist_locatable_axes_001.png\n :target: ../../gallery/axes_grid1/scatter_hist_locatable_axes.html\n :align: center\n :scale: 50\n\n Scatter Hist\n\nThe :doc:`/gallery/axes_grid1/scatter_hist_locatable_axes` using the\nAxesDivider has some advantage over the\noriginal :doc:`/gallery/lines_bars_and_markers/scatter_hist` in Matplotlib.\nFor example, you can set the aspect ratio of the scatter plot, even with the\nx-axis or y-axis is shared accordingly.\n\n\nParasiteAxes\n------------\n\nThe ParasiteAxes is an axes whose location is identical to its host\naxes. The location is adjusted in the drawing time, thus it works even\nif the host change its location (e.g., images).\n\nIn most cases, you first create a host axes, which provides a few\nmethod that can be used to create parasite axes. They are *twinx*,\n*twiny* (which are similar to twinx and twiny in the matplotlib) and\n*twin*. *twin* takes an arbitrary transformation that maps between the\ndata coordinates of the host axes and the parasite axes. *draw*\nmethod of the parasite axes are never called. Instead, host axes\ncollects artists in parasite axes and draw them as if they belong to\nthe host axes, i.e., artists in parasite axes are merged to those of\nthe host axes and then drawn according to their zorder. The host and\nparasite axes modifies some of the axes behavior. For example, color\ncycle for plot lines are shared between host and parasites. Also, the\nlegend command in host, creates a legend that includes lines in the\nparasite axes. To create a host axes, you may use *host_subplot* or\n*host_axes* command.\n\n\nExample 1. twinx\n~~~~~~~~~~~~~~~~\n\n.. figure:: ../../gallery/axes_grid1/images/sphx_glr_parasite_simple_001.png\n :target: ../../gallery/axes_grid1/parasite_simple.html\n :align: center\n :scale: 50\n\n Parasite Simple\n\n\nExample 2. twin\n~~~~~~~~~~~~~~~\n\n*twin* without a transform argument assumes that the parasite axes has the\nsame data transform as the host. This can be useful when you want the\ntop(or right)-axis to have different tick-locations, tick-labels, or\ntick-formatter for bottom(or left)-axis. ::\n\n ax2 = ax.twin() # now, ax2 is responsible for \"top\" axis and \"right\" axis\n ax2.set_xticks([0., .5*np.pi, np.pi, 1.5*np.pi, 2*np.pi])\n ax2.set_xticklabels([\"0\", r\"$\\frac{1}{2}\\pi$\",\n r\"$\\pi$\", r\"$\\frac{3}{2}\\pi$\", r\"$2\\pi$\"])\n\n\n.. figure:: ../../gallery/axes_grid1/images/sphx_glr_simple_axisline4_001.png\n :target: ../../gallery/axes_grid1/simple_axisline4.html\n :align: center\n :scale: 50\n\n Simple Axisline4\n\nA more sophisticated example using twin. Note that if you change the\nx-limit in the host axes, the x-limit of the parasite axes will change\naccordingly.\n\n\n.. figure:: ../../gallery/axes_grid1/images/sphx_glr_parasite_simple2_001.png\n :target: ../../gallery/axes_grid1/parasite_simple2.html\n :align: center\n :scale: 50\n\n Parasite Simple2\n\n\nAnchoredArtists\n---------------\n\nIt's a collection of artists whose location is anchored to the (axes)\nbbox, like the legend. It is derived from *OffsetBox* in Matplotlib, and\nartist need to be drawn in the canvas coordinate. But, there is a\nlimited support for an arbitrary transform. For example, the ellipse\nin the example below will have width and height in the data\ncoordinate.\n\n.. figure:: ../../gallery/axes_grid1/images/sphx_glr_simple_anchored_artists_001.png\n :target: ../../gallery/axes_grid1/simple_anchored_artists.html\n :align: center\n :scale: 50\n\n Simple Anchored Artists\n\n\nInsetLocator\n------------\n\n:mod:`mpl_toolkits.axes_grid1.inset_locator` provides helper classes\nand functions to place your (inset) axes at the anchored position of\nthe parent axes, similarly to AnchoredArtist.\n\nUsing :func:`mpl_toolkits.axes_grid1.inset_locator.inset_axes`, you\ncan have inset axes whose size is either fixed, or a fixed proportion\nof the parent axes::\n\n inset_axes = inset_axes(parent_axes,\n width=\"30%\", # width = 30% of parent_bbox\n height=1., # height : 1 inch\n loc='lower left')\n\ncreates an inset axes whose width is 30% of the parent axes and whose\nheight is fixed at 1 inch.\n\nYou may creates your inset whose size is determined so that the data\nscale of the inset axes to be that of the parent axes multiplied by\nsome factor. For example, ::\n\n inset_axes = zoomed_inset_axes(ax,\n 0.5, # zoom = 0.5\n loc='upper right')\n\ncreates an inset axes whose data scale is half of the parent axes.\nHere is complete examples.\n\n.. figure:: ../../gallery/axes_grid1/images/sphx_glr_inset_locator_demo_001.png\n :target: ../../gallery/axes_grid1/inset_locator_demo.html\n :align: center\n :scale: 50\n\n Inset Locator Demo\n\nFor example, :func:`.zoomed_inset_axes` can be used when you want the\ninset represents the zoom-up of the small portion in the parent axes.\nAnd :mod:`~mpl_toolkits.axes_grid1.inset_locator` provides a helper\nfunction :func:`.mark_inset` to mark the location of the area\nrepresented by the inset axes.\n\n.. figure:: ../../gallery/axes_grid1/images/sphx_glr_inset_locator_demo2_001.png\n :target: ../../gallery/axes_grid1/inset_locator_demo2.html\n :align: center\n :scale: 50\n\n Inset Locator Demo2\n\n\nRGB Axes\n~~~~~~~~\n\nRGBAxes is a helper class to conveniently show RGB composite\nimages. Like ImageGrid, the location of axes are adjusted so that the\narea occupied by them fits in a given rectangle. Also, the xaxis and\nyaxis of each axes are shared. ::\n\n from mpl_toolkits.axes_grid1.axes_rgb import RGBAxes\n\n fig = plt.figure()\n ax = RGBAxes(fig, [0.1, 0.1, 0.8, 0.8], pad=0.0)\n r, g, b = get_rgb() # r, g, b are 2D images.\n ax.imshow_rgb(r, g, b)\n\n\n.. figure:: ../../gallery/axes_grid1/images/sphx_glr_demo_axes_rgb_001.png\n :target: ../../gallery/axes_grid1/demo_axes_rgb.html\n :align: center\n :scale: 50\n\n\nAxesDivider\n===========\n\nThe :mod:`mpl_toolkits.axes_grid1.axes_divider` module provides helper classes\nto adjust the axes positions of a set of images at drawing time.\n\n* :mod:`~mpl_toolkits.axes_grid1.axes_size` provides a class of\n units that are used to determine the size of each axes. For example,\n you can specify a fixed size.\n\n* `~mpl_toolkits.axes_grid1.axes_divider.Divider` is the class that\n calculates the axes position. It divides the given rectangular area into\n several areas. The divider is initialized by setting the lists of horizontal\n and vertical sizes on which the division will be based. Then use\n :meth:`~mpl_toolkits.axes_grid1.axes_divider.Divider.new_locator`, which\n returns a callable object that can be used to set the axes_locator of the\n axes.\n\nHere, we demonstrate how to achieve the following layout: we want to position\naxes in a 3x4 grid (note that `.Divider` makes row indices start from the\n*bottom*\\(!) of the grid):\n\n.. code-block:: none\n\n +--------+--------+--------+--------+\n | (2, 0) | (2, 1) | (2, 2) | (2, 3) |\n +--------+--------+--------+--------+\n | (1, 0) | (1, 1) | (1, 2) | (1, 3) |\n +--------+--------+--------+--------+\n | (0, 0) | (0, 1) | (0, 2) | (0, 3) |\n +--------+--------+--------+--------+\n\nsuch that the bottom row has a fixed height of 2 (inches) and the top two rows\nhave a height ratio of 2 (middle) to 3 (top). (For example, if the grid has\na size of 7 inches, the bottom row will be 2 inches, the middle row also 2\ninches, and the top row 3 inches.)\n\nThese constraints are specified using classes from the\n:mod:`~mpl_toolkits.axes_grid1.axes_size` module, namely::\n\n from mpl_toolkits.axes_grid1.axes_size import Fixed, Scaled\n vert = [Fixed(2), Scaled(2), Scaled(3)]\n\n(More generally, :mod:`~mpl_toolkits.axes_grid1.axes_size` classes define a\n``get_size(renderer)`` method that returns a pair of floats -- a relative size,\nand an absolute size. ``Fixed(2).get_size(renderer)`` returns ``(0, 2)``;\n``Scaled(2).get_size(renderer)`` returns ``(2, 0)``.)\n\nWe use these constraints to initialize a `.Divider` object::\n\n rect = [0.2, 0.2, 0.6, 0.6] # Position of the grid in the figure.\n vert = [Fixed(2), Scaled(2), Scaled(3)] # As above.\n horiz = [...] # Some other horizontal constraints.\n divider = Divider(fig, rect, horiz, vert)\n\nthen use `.Divider.new_locator` to create an `.AxesLocator` instance for a\ngiven grid entry::\n\n locator = divider.new_locator(nx=0, ny=1) # Grid entry (1, 0).\n\nand make it responsible for locating the axes::\n\n ax.set_axes_locator(locator)\n\nThe `.AxesLocator` is a callable object that returns the location and size of\nthe cell at the first column and the second row.\n\nLocators that spans over multiple cells can be created with, e.g.::\n\n # Columns #0 and #1 (\"0-2 range\"), row #1.\n locator = divider.new_locator(nx=0, nx1=2, ny=1)\n\nSee the example,\n\n.. figure:: ../../gallery/axes_grid1/images/sphx_glr_simple_axes_divider1_002.png\n :target: ../../gallery/axes_grid1/simple_axes_divider1.html\n :align: center\n :scale: 50\n\nYou can also adjust the size of each axes according to its x or y\ndata limits (AxesX and AxesY).\n\n.. figure:: ../../gallery/axes_grid1/images/sphx_glr_simple_axes_divider3_001.png\n :target: ../../gallery/axes_grid1/simple_axes_divider3.html\n :align: center\n :scale: 50\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.3"
}
},
"nbformat": 4,
"nbformat_minor": 0
}