.. 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_gallery_ticks_and_spines_colorbar_tick_labelling_demo.py:
============================
Colorbar Tick Labelling Demo
============================
Produce custom labelling for a colorbar.
Contributed by Scott Sinclair
.. code-block:: default
import matplotlib.pyplot as plt
import numpy as np
from matplotlib import cm
from numpy.random import randn
# Fixing random state for reproducibility
np.random.seed(19680801)
Make plot with vertical (default) colorbar
.. code-block:: default
fig, ax = plt.subplots()
data = np.clip(randn(250, 250), -1, 1)
cax = ax.imshow(data, cmap=cm.coolwarm)
ax.set_title('Gaussian noise with vertical colorbar')
# Add colorbar, make sure to specify tick locations to match desired ticklabels
cbar = fig.colorbar(cax, ticks=[-1, 0, 1])
cbar.ax.set_yticklabels(['< -1', '0', '> 1']) # vertically oriented colorbar
.. image:: /gallery/ticks_and_spines/images/sphx_glr_colorbar_tick_labelling_demo_001.png
:alt: Gaussian noise with vertical colorbar
:class: sphx-glr-single-img
.. rst-class:: sphx-glr-script-out
Out:
.. code-block:: none
[Text(1, -1, '< -1'), Text(1, 0, '0'), Text(1, 1, '> 1')]
Make plot with horizontal colorbar
.. code-block:: default
fig, ax = plt.subplots()
data = np.clip(randn(250, 250), -1, 1)
cax = ax.imshow(data, cmap=cm.afmhot)
ax.set_title('Gaussian noise with horizontal colorbar')
cbar = fig.colorbar(cax, ticks=[-1, 0, 1], orientation='horizontal')
cbar.ax.set_xticklabels(['Low', 'Medium', 'High']) # horizontal colorbar
plt.show()
.. image:: /gallery/ticks_and_spines/images/sphx_glr_colorbar_tick_labelling_demo_002.png
:alt: Gaussian noise with horizontal colorbar
:class: sphx-glr-single-img
.. _sphx_glr_download_gallery_ticks_and_spines_colorbar_tick_labelling_demo.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_tick_labelling_demo.py `
.. container:: sphx-glr-download sphx-glr-download-jupyter
:download:`Download Jupyter notebook: colorbar_tick_labelling_demo.ipynb `
.. only:: html
.. rst-class:: sphx-glr-signature
Keywords: matplotlib code example, codex, python plot, pyplot
`Gallery generated by Sphinx-Gallery
`_