
.. DO NOT EDIT.
.. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY.
.. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE:
.. "gallery/axes_grid1/demo_edge_colorbar.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_edge_colorbar.py>`
        to download the full example code.

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

.. _sphx_glr_gallery_axes_grid1_demo_edge_colorbar.py:


===============================
Per-row or per-column colorbars
===============================

This example shows how to use one common colorbar for each row or column
of an image grid.

.. GENERATED FROM PYTHON SOURCE LINES 9-87

.. code-block:: Python


    import matplotlib.pyplot as plt

    from matplotlib import cbook
    from mpl_toolkits.axes_grid1 import AxesGrid


    def get_demo_image():
        z = cbook.get_sample_data("axes_grid/bivariate_normal.npy")  # 15x15 array
        return z, (-3, 4, -4, 3)


    def demo_bottom_cbar(fig):
        """
        A grid of 2x2 images with a colorbar for each column.
        """
        grid = AxesGrid(fig, 121,  # similar to subplot(121)
                        nrows_ncols=(2, 2),
                        axes_pad=0.10,
                        share_all=True,
                        label_mode="1",
                        cbar_location="bottom",
                        cbar_mode="edge",
                        cbar_pad=0.25,
                        cbar_size="15%",
                        direction="column"
                        )

        Z, extent = get_demo_image()
        cmaps = ["autumn", "summer"]
        for i in range(4):
            im = grid[i].imshow(Z, extent=extent, cmap=cmaps[i//2])
            if i % 2:
                grid.cbar_axes[i//2].colorbar(im)

        for cax in grid.cbar_axes:
            cax.axis[cax.orientation].set_label("Bar")

        # This affects all Axes as share_all = True.
        grid.axes_llc.set_xticks([-2, 0, 2])
        grid.axes_llc.set_yticks([-2, 0, 2])


    def demo_right_cbar(fig):
        """
        A grid of 2x2 images. Each row has its own colorbar.
        """
        grid = AxesGrid(fig, 122,  # similar to subplot(122)
                        nrows_ncols=(2, 2),
                        axes_pad=0.10,
                        label_mode="1",
                        share_all=True,
                        cbar_location="right",
                        cbar_mode="edge",
                        cbar_size="7%",
                        cbar_pad="2%",
                        )
        Z, extent = get_demo_image()
        cmaps = ["spring", "winter"]
        for i in range(4):
            im = grid[i].imshow(Z, extent=extent, cmap=cmaps[i//2])
            if i % 2:
                grid.cbar_axes[i//2].colorbar(im)

        for cax in grid.cbar_axes:
            cax.axis[cax.orientation].set_label('Foo')

        # This affects all Axes because we set share_all = True.
        grid.axes_llc.set_xticks([-2, 0, 2])
        grid.axes_llc.set_yticks([-2, 0, 2])


    fig = plt.figure()

    demo_bottom_cbar(fig)
    demo_right_cbar(fig)

    plt.show()


.. _sphx_glr_download_gallery_axes_grid1_demo_edge_colorbar.py:

.. only:: html

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

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

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

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

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

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

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


.. only:: html

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

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