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

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

.. _sphx_glr_gallery_color_colorbar_basics.py:


========
Colorbar
========

Use `~.Figure.colorbar` by specifying the mappable object (here
the `.AxesImage` returned by `~.axes.Axes.imshow`)
and the Axes to attach the colorbar to.

.. GENERATED FROM PYTHON SOURCE LINES 10-48

.. code-block:: Python


    import matplotlib.pyplot as plt
    import numpy as np

    # setup some generic data
    N = 37
    x, y = np.mgrid[:N, :N]
    Z = (np.cos(x*0.2) + np.sin(y*0.3))

    # mask out the negative and positive values, respectively
    Zpos = np.ma.masked_less(Z, 0)
    Zneg = np.ma.masked_greater(Z, 0)

    fig, (ax1, ax2, ax3) = plt.subplots(figsize=(13, 3), ncols=3)

    # plot just the positive data and save the
    # color "mappable" object returned by ax1.imshow
    pos = ax1.imshow(Zpos, cmap='Blues', interpolation='none')

    # add the colorbar using the figure's method,
    # telling which mappable we're talking about and
    # which Axes object it should be near
    fig.colorbar(pos, ax=ax1)

    # repeat everything above for the negative data
    # you can specify location, anchor and shrink the colorbar
    neg = ax2.imshow(Zneg, cmap='Reds_r', interpolation='none')
    fig.colorbar(neg, ax=ax2, location='right', anchor=(0, 0.3), shrink=0.7)

    # Plot both positive and negative values between +/- 1.2
    pos_neg_clipped = ax3.imshow(Z, cmap='RdBu', vmin=-1.2, vmax=1.2,
                                 interpolation='none')
    # Add minorticks on the colorbar to make it easy to read the
    # values off the colorbar.
    cbar = fig.colorbar(pos_neg_clipped, ax=ax3, extend='both')
    cbar.minorticks_on()
    plt.show()




.. image-sg:: /gallery/color/images/sphx_glr_colorbar_basics_001.png
   :alt: colorbar basics
   :srcset: /gallery/color/images/sphx_glr_colorbar_basics_001.png, /gallery/color/images/sphx_glr_colorbar_basics_001_2_00x.png 2.00x
   :class: sphx-glr-single-img





.. GENERATED FROM PYTHON SOURCE LINES 49-65

.. 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.colorbar.Colorbar.minorticks_on`
   - `matplotlib.colorbar.Colorbar.minorticks_off`

.. tags::

   component: colorbar
   styling: color
   plot-type: imshow
   level: beginner


.. _sphx_glr_download_gallery_color_colorbar_basics.py:

.. only:: html

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

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

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

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

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

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

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


.. only:: html

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

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