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

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

.. _sphx_glr_gallery_images_contours_and_fields_multi_image.py:


=================================
Multiple images with one colorbar
=================================

Use a single colorbar for multiple images.

Currently, a colorbar can only be connected to one image. The connection
guarantees that the data coloring is consistent with the colormap scale
(i.e. the color of value *x* in the colormap is used for coloring a data
value *x* in the image).

If we want one colorbar to be representative for multiple images, we have
to explicitly ensure consistent data coloring by using the same data
normalization for all the images. We ensure this by explicitly creating a
``norm`` object that we pass to all the image plotting methods.

.. GENERATED FROM PYTHON SOURCE LINES 18-45

.. code-block:: Python


    import matplotlib.pyplot as plt
    import numpy as np

    from matplotlib import colors

    np.random.seed(19680801)

    datasets = [
        (i+1)/10 * np.random.rand(10, 20)
        for i in range(4)
    ]

    fig, axs = plt.subplots(2, 2)
    fig.suptitle('Multiple images')

    # create a single norm to be shared across all images
    norm = colors.Normalize(vmin=np.min(datasets), vmax=np.max(datasets))

    images = []
    for ax, data in zip(axs.flat, datasets):
        images.append(ax.imshow(data, norm=norm))

    fig.colorbar(images[0], ax=axs, orientation='horizontal', fraction=.1)

    plt.show()




.. image-sg:: /gallery/images_contours_and_fields/images/sphx_glr_multi_image_001.png
   :alt: Multiple images
   :srcset: /gallery/images_contours_and_fields/images/sphx_glr_multi_image_001.png, /gallery/images_contours_and_fields/images/sphx_glr_multi_image_001_2_00x.png 2.00x
   :class: sphx-glr-single-img





.. GENERATED FROM PYTHON SOURCE LINES 46-83

The colors are now kept consistent across all images when changing the
scaling, e.g. through zooming in the colorbar or via the "edit axis,
curves and images parameters" GUI of the Qt backend. This is sufficient
for most practical use cases.

Advanced: Additionally sync the colormap
----------------------------------------

Sharing a common norm object guarantees synchronized scaling because scale
changes modify the norm object in-place and thus propagate to all images
that use this norm. This approach does not help with synchronizing colormaps
because changing the colormap of an image (e.g. through the "edit axis,
curves and images parameters" GUI of the Qt backend) results in the image
referencing the new colormap object. Thus, the other images are not updated.

To update the other images, sync the
colormaps using the following code::

    def sync_cmaps(changed_image):
        for im in images:
            if changed_image.get_cmap() != im.get_cmap():
                im.set_cmap(changed_image.get_cmap())

    for im in images:
        im.callbacks.connect('changed', sync_cmaps)


.. admonition:: References

   The use of the following functions, methods, classes and modules is shown
   in this example:

   - `matplotlib.axes.Axes.imshow` / `matplotlib.pyplot.imshow`
   - `matplotlib.figure.Figure.colorbar` / `matplotlib.pyplot.colorbar`
   - `matplotlib.colors.Normalize`
   - `matplotlib.cm.ScalarMappable.set_cmap`
   - `matplotlib.cbook.CallbackRegistry.connect`


.. _sphx_glr_download_gallery_images_contours_and_fields_multi_image.py:

.. only:: html

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

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

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

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

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

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

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


.. only:: html

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

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