

.. _sphx_glr_gallery_userdemo_colormap_normalizations_custom.py:


==============================
Colormap Normalizations Custom
==============================

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




.. image:: /gallery/userdemo/images/sphx_glr_colormap_normalizations_custom_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
    '''
    Custom Norm: An example with a customized normalization.  This one
    uses the example above, and normalizes the negative data differently
    from the positive.
    '''
    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

    # Example of making your own norm.  Also see matplotlib.colors.
    # From Joe Kington: This one gives two different linear ramps:


    class MidpointNormalize(colors.Normalize):
        def __init__(self, vmin=None, vmax=None, midpoint=None, clip=False):
            self.midpoint = midpoint
            colors.Normalize.__init__(self, vmin, vmax, clip)

        def __call__(self, value, clip=None):
            # I'm ignoring masked values and all kinds of edge cases to make a
            # simple example...
            x, y = [self.vmin, self.midpoint, self.vmax], [0, 0.5, 1]
            return np.ma.masked_array(np.interp(value, x, y))
    #####
    fig, ax = plt.subplots(2, 1)

    pcm = ax[0].pcolormesh(X, Y, Z1,
                           norm=MidpointNormalize(midpoint=0.),
                           cmap='RdBu_r')
    fig.colorbar(pcm, ax=ax[0], extend='both')

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

    plt.show()

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



.. only :: html

 .. container:: sphx-glr-footer


  .. container:: sphx-glr-download

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



  .. container:: sphx-glr-download

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


.. only:: html

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

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