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

    Click :ref:`here <sphx_glr_download_gallery_subplots_axes_and_figures_colorbar_placement.py>` to download the full example code
.. rst-class:: sphx-glr-example-title

.. _sphx_glr_gallery_subplots_axes_and_figures_colorbar_placement.py:


=================
Placing Colorbars
=================

Colorbars indicate the quantitative extent of image data.  Placing in
a figure is non-trivial because room needs to be made for them.

The simplest case is just attaching a colorbar to each axes:



.. code-block:: python

    import matplotlib.pyplot as plt
    import numpy as np

    fig, axs = plt.subplots(2, 2)
    cm = ['RdBu_r', 'viridis']
    for col in range(2):
        for row in range(2):
            ax = axs[row, col]
            pcm = ax.pcolormesh(np.random.random((20, 20)) * (col + 1),
                                cmap=cm[col])
            fig.colorbar(pcm, ax=ax)
    plt.show()




.. image:: /gallery/subplots_axes_and_figures/images/sphx_glr_colorbar_placement_001.png
    :class: sphx-glr-single-img




The first column has the same type of data in both rows, so it may
be desirable to combine the colorbar which we do by calling
`.Figure.colorbar` with a list of axes instead of a single axes.



.. code-block:: python


    fig, axs = plt.subplots(2, 2)
    cm = ['RdBu_r', 'viridis']
    for col in range(2):
        for row in range(2):
            ax = axs[row, col]
            pcm = ax.pcolormesh(np.random.random((20, 20)) * (col + 1),
                                cmap=cm[col])
        fig.colorbar(pcm, ax=axs[:, col], shrink=0.6)
    plt.show()




.. image:: /gallery/subplots_axes_and_figures/images/sphx_glr_colorbar_placement_002.png
    :class: sphx-glr-single-img




Relatively complicated colorbar layouts are possible using this
paradigm.  Note that this example works far better with
``constrained_layout=True``



.. code-block:: python


    fig, axs = plt.subplots(3, 3, constrained_layout=True)
    for ax in axs.flat:
        pcm = ax.pcolormesh(np.random.random((20, 20)))

    fig.colorbar(pcm, ax=axs[0, :2], shrink=0.6, location='bottom')
    fig.colorbar(pcm, ax=[axs[0, 2]], location='bottom')
    fig.colorbar(pcm, ax=axs[1:, :], location='right', shrink=0.6)
    fig.colorbar(pcm, ax=[axs[2, 1]], location='left')


    plt.show()



.. image:: /gallery/subplots_axes_and_figures/images/sphx_glr_colorbar_placement_003.png
    :class: sphx-glr-single-img





.. _sphx_glr_download_gallery_subplots_axes_and_figures_colorbar_placement.py:


.. only :: html

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



  .. container:: sphx-glr-download

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



  .. container:: sphx-glr-download

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


.. only:: html

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

    Keywords: matplotlib code example, codex, python plot, pyplot
    `Gallery generated by Sphinx-Gallery
    <https://sphinx-gallery.readthedocs.io>`_
