
.. DO NOT EDIT.
.. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY.
.. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE:
.. "gallery/axes_grid1/demo_axes_grid2.py"
.. LINE NUMBERS ARE GIVEN BELOW.

.. only:: html

    .. meta::
        :keywords: codex

    .. note::
        :class: sphx-glr-download-link-note

        :ref:`Go to the end <sphx_glr_download_gallery_axes_grid1_demo_axes_grid2.py>`
        to download the full example code.

.. rst-class:: sphx-glr-example-title

.. _sphx_glr_gallery_axes_grid1_demo_axes_grid2.py:


==========
Axes Grid2
==========

Grid of images with shared xaxis and yaxis.

.. GENERATED FROM PYTHON SOURCE LINES 8-70

.. code-block:: Python


    import matplotlib.pyplot as plt
    import numpy as np

    from matplotlib import cbook
    from mpl_toolkits.axes_grid1 import ImageGrid


    def add_inner_title(ax, title, loc, **kwargs):
        from matplotlib.offsetbox import AnchoredText
        from matplotlib.patheffects import withStroke
        prop = dict(path_effects=[withStroke(foreground='w', linewidth=3)],
                    size=plt.rcParams['legend.fontsize'])
        at = AnchoredText(title, loc=loc, prop=prop,
                          pad=0., borderpad=0.5,
                          frameon=False, **kwargs)
        ax.add_artist(at)
        return at


    fig = plt.figure(figsize=(6, 6))

    # Prepare images
    Z = cbook.get_sample_data("axes_grid/bivariate_normal.npy")
    extent = (-3, 4, -4, 3)
    ZS = [Z[i::3, :] for i in range(3)]
    extent = extent[0], extent[1]/3., extent[2], extent[3]

    # *** Demo 1: colorbar at each Axes ***
    grid = ImageGrid(
        # 211 = at the position of fig.add_subplot(211)
        fig, 211, nrows_ncols=(1, 3), axes_pad=0.05, label_mode="1", share_all=True,
        cbar_location="top", cbar_mode="each", cbar_size="7%", cbar_pad="1%")
    grid[0].set(xticks=[-2, 0], yticks=[-2, 0, 2])

    for i, (ax, z) in enumerate(zip(grid, ZS)):
        im = ax.imshow(z, origin="lower", extent=extent)
        cb = ax.cax.colorbar(im)
        # Changing the colorbar ticks
        if i in [1, 2]:
            cb.set_ticks([-1, 0, 1])

    for ax, im_title in zip(grid, ["Image 1", "Image 2", "Image 3"]):
        add_inner_title(ax, im_title, loc='lower left')

    # *** Demo 2: shared colorbar ***
    grid2 = ImageGrid(
        fig, 212, nrows_ncols=(1, 3), axes_pad=0.05, label_mode="1", share_all=True,
        cbar_location="right", cbar_mode="single", cbar_size="10%", cbar_pad=0.05)
    grid2[0].set(xlabel="X", ylabel="Y", xticks=[-2, 0], yticks=[-2, 0, 2])

    clim = (np.min(ZS), np.max(ZS))
    for ax, z in zip(grid2, ZS):
        im = ax.imshow(z, clim=clim, origin="lower", extent=extent)

    # With cbar_mode="single", cax attribute of all Axes are identical.
    ax.cax.colorbar(im)

    for ax, im_title in zip(grid2, ["(a)", "(b)", "(c)"]):
        add_inner_title(ax, im_title, loc='upper left')

    plt.show()


.. _sphx_glr_download_gallery_axes_grid1_demo_axes_grid2.py:

.. only:: html

  .. container:: sphx-glr-footer sphx-glr-footer-example

    .. container:: sphx-glr-download sphx-glr-download-jupyter

      :download:`Download Jupyter notebook: demo_axes_grid2.ipynb <demo_axes_grid2.ipynb>`

    .. container:: sphx-glr-download sphx-glr-download-python

      :download:`Download Python source code: demo_axes_grid2.py <demo_axes_grid2.py>`

    .. container:: sphx-glr-download sphx-glr-download-zip

      :download:`Download zipped: demo_axes_grid2.zip <demo_axes_grid2.zip>`


.. only:: html

 .. rst-class:: sphx-glr-signature

    `Gallery generated by Sphinx-Gallery <https://sphinx-gallery.github.io>`_
