

.. _sphx_glr_gallery_pylab_examples_colorbar_tick_labelling_demo.py:


============================
Colorbar Tick Labelling Demo
============================

Produce custom labelling for a colorbar.

Contributed by Scott Sinclair




.. rst-class:: sphx-glr-horizontal


    *

      .. image:: /gallery/pylab_examples/images/sphx_glr_colorbar_tick_labelling_demo_001.png
            :scale: 47

    *

      .. image:: /gallery/pylab_examples/images/sphx_glr_colorbar_tick_labelling_demo_002.png
            :scale: 47





.. code-block:: python


    import matplotlib.pyplot as plt
    import numpy as np
    from matplotlib import cm
    from numpy.random import randn

    # Make plot with vertical (default) colorbar
    fig, ax = plt.subplots()

    data = np.clip(randn(250, 250), -1, 1)

    cax = ax.imshow(data, interpolation='nearest', 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

    # Make plot with horizontal colorbar
    fig, ax = plt.subplots()

    data = np.clip(randn(250, 250), -1, 1)

    cax = ax.imshow(data, interpolation='nearest', 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()

**Total running time of the script:** ( 0 minutes  0.101 seconds)



.. container:: sphx-glr-footer


  .. container:: sphx-glr-download

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



  .. container:: sphx-glr-download

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

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

    `Generated by Sphinx-Gallery <https://sphinx-gallery.readthedocs.io>`_
