

.. _sphx_glr_gallery_userdemo_colormap_normalizations_bounds.py:


==============================
Colormap Normalizations Bounds
==============================

Demonstration of using norm to map colormaps onto data in non-linear ways.




.. image:: /gallery/userdemo/images/sphx_glr_colormap_normalizations_bounds_001.png
    :align: center





.. code-block:: python


    import numpy as np
    import matplotlib.pyplot as plt
    import matplotlib.colors as colors
    from matplotlib.mlab import bivariate_normal

    N = 100
    X, Y = np.mgrid[-3:3:complex(0, N), -2:2:complex(0, N)]
    Z1 = (bivariate_normal(X, Y, 1., 1., 1.0, 1.0))**2  \
        - 0.4 * (bivariate_normal(X, Y, 1.0, 1.0, -1.0, 0.0))**2
    Z1 = Z1/0.03

    '''
    BoundaryNorm: For this one you provide the boundaries for your colors,
    and the Norm puts the first color in between the first pair, the
    second color between the second pair, etc.
    '''

    fig, ax = plt.subplots(3, 1, figsize=(8, 8))
    ax = ax.flatten()
    # even bounds gives a contour-like effect
    bounds = np.linspace(-1, 1, 10)
    norm = colors.BoundaryNorm(boundaries=bounds, ncolors=256)
    pcm = ax[0].pcolormesh(X, Y, Z1,
                           norm=norm,
                           cmap='RdBu_r')
    fig.colorbar(pcm, ax=ax[0], extend='both', orientation='vertical')

    # uneven bounds changes the colormapping:
    bounds = np.array([-0.25, -0.125, 0, 0.5, 1])
    norm = colors.BoundaryNorm(boundaries=bounds, ncolors=256)
    pcm = ax[1].pcolormesh(X, Y, Z1, norm=norm, cmap='RdBu_r')
    fig.colorbar(pcm, ax=ax[1], extend='both', orientation='vertical')

    pcm = ax[2].pcolormesh(X, Y, Z1, cmap='RdBu_r', vmin=-np.max(Z1))
    fig.colorbar(pcm, ax=ax[2], extend='both', orientation='vertical')

    plt.show()

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



.. only :: html

 .. container:: sphx-glr-footer


  .. container:: sphx-glr-download

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



  .. container:: sphx-glr-download

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


.. only:: html

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

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