.. only:: html
.. note::
:class: sphx-glr-download-link-note
Click :ref:`here ` to download the full example code
.. rst-class:: sphx-glr-example-title
.. _sphx_glr_tutorials_colors_colorbar_only.py:
=============================
Customized Colorbars Tutorial
=============================
This tutorial shows how to build and customize standalone colorbars, i.e.
without an attached plot.
Customized Colorbars
====================
A `~.Figure.colorbar` needs a "mappable" (`matplotlib.cm.ScalarMappable`)
object (typically, an image) which indicates the colormap and the norm to be
used. In order to create a colorbar without an attached image, one can instead
use a `.ScalarMappable` with no associated data.
Basic continuous colorbar
-------------------------
Here we create a basic continuous colorbar with ticks and labels.
The arguments to the `~.Figure.colorbar` call are the `.ScalarMappable`
(constructed using the *norm* and *cmap* arguments), the axes where the
colorbar should be drawn, and the colorbar's orientation.
For more information see the :mod:`~matplotlib.colorbar` API.
.. code-block:: default
import matplotlib.pyplot as plt
import matplotlib as mpl
fig, ax = plt.subplots(figsize=(6, 1))
fig.subplots_adjust(bottom=0.5)
cmap = mpl.cm.cool
norm = mpl.colors.Normalize(vmin=5, vmax=10)
fig.colorbar(mpl.cm.ScalarMappable(norm=norm, cmap=cmap),
cax=ax, orientation='horizontal', label='Some Units')
.. image:: /tutorials/colors/images/sphx_glr_colorbar_only_001.png
:class: sphx-glr-single-img
.. rst-class:: sphx-glr-script-out
Out:
.. code-block:: none
Discrete intervals colorbar
---------------------------
The second example illustrates the use of a
:class:`~matplotlib.colors.ListedColormap` which generates a colormap from a
set of listed colors, `.colors.BoundaryNorm` which generates a colormap
index based on discrete intervals and extended ends to show the "over" and
"under" value colors. Over and under are used to display data outside of the
normalized [0, 1] range. Here we pass colors as gray shades as a string
encoding a float in the 0-1 range.
If a :class:`~matplotlib.colors.ListedColormap` is used, the length of the
bounds array must be one greater than the length of the color list. The
bounds must be monotonically increasing.
This time we pass some more arguments in addition to previous arguments to
`~.Figure.colorbar`. For the out-of-range values to
display on the colorbar, we have to use the *extend* keyword argument. To use
*extend*, you must specify two extra boundaries. Finally spacing argument
ensures that intervals are shown on colorbar proportionally.
.. code-block:: default
fig, ax = plt.subplots(figsize=(6, 1))
fig.subplots_adjust(bottom=0.5)
cmap = mpl.colors.ListedColormap(['red', 'green', 'blue', 'cyan'])
cmap.set_over('0.25')
cmap.set_under('0.75')
bounds = [1, 2, 4, 7, 8]
norm = mpl.colors.BoundaryNorm(bounds, cmap.N)
fig.colorbar(
mpl.cm.ScalarMappable(cmap=cmap, norm=norm),
cax=ax,
boundaries=[0] + bounds + [13],
extend='both',
ticks=bounds,
spacing='proportional',
orientation='horizontal',
label='Discrete intervals, some other units',
)
.. image:: /tutorials/colors/images/sphx_glr_colorbar_only_002.png
:class: sphx-glr-single-img
.. rst-class:: sphx-glr-script-out
Out:
.. code-block:: none
Colorbar with custom extension lengths
--------------------------------------
Here we illustrate the use of custom length colorbar extensions, used on a
colorbar with discrete intervals. To make the length of each extension the
same as the length of the interior colors, use ``extendfrac='auto'``.
.. code-block:: default
fig, ax = plt.subplots(figsize=(6, 1))
fig.subplots_adjust(bottom=0.5)
cmap = mpl.colors.ListedColormap(['royalblue', 'cyan',
'yellow', 'orange'])
cmap.set_over('red')
cmap.set_under('blue')
bounds = [-1.0, -0.5, 0.0, 0.5, 1.0]
norm = mpl.colors.BoundaryNorm(bounds, cmap.N)
fig.colorbar(
mpl.cm.ScalarMappable(cmap=cmap, norm=norm),
cax=ax,
boundaries=[-10] + bounds + [10],
extend='both',
extendfrac='auto',
ticks=bounds,
spacing='uniform',
orientation='horizontal',
label='Custom extension lengths, some other units',
)
plt.show()
.. image:: /tutorials/colors/images/sphx_glr_colorbar_only_003.png
:class: sphx-glr-single-img
.. _sphx_glr_download_tutorials_colors_colorbar_only.py:
.. only :: html
.. container:: sphx-glr-footer
:class: sphx-glr-footer-example
.. container:: sphx-glr-download sphx-glr-download-python
:download:`Download Python source code: colorbar_only.py `
.. container:: sphx-glr-download sphx-glr-download-jupyter
:download:`Download Jupyter notebook: colorbar_only.ipynb `
.. only:: html
.. rst-class:: sphx-glr-signature
Keywords: matplotlib code example, codex, python plot, pyplot
`Gallery generated by Sphinx-Gallery
`_