Department of Physics and Astronomy

The Forbes Group

Colormaps

$\newcommand{\vect}[1]{\mathbf{#1}} \newcommand{\uvect}[1]{\hat{#1}} \newcommand{\abs}[1]{\lvert#1\rvert} \newcommand{\norm}[1]{\lVert#1\rVert} \newcommand{\I}{\mathrm{i}} \newcommand{\ket}[1]{\left|#1\right\rangle} \newcommand{\bra}[1]{\left\langle#1\right|} \newcommand{\braket}[1]{\langle#1\rangle} \newcommand{\op}[1]{\mathbf{#1}} \newcommand{\mat}[1]{\mathbf{#1}} \newcommand{\d}{\mathrm{d}} \newcommand{\pdiff}[3][]{\frac{\partial^{#1} #2}{\partial {#3}^{#1}}} \newcommand{\diff}[3][]{\frac{\d^{#1} #2}{\d {#3}^{#1}}} \newcommand{\ddiff}[3][]{\frac{\delta^{#1} #2}{\delta {#3}^{#1}}} \DeclareMathOperator{\erf}{erf} \DeclareMathOperator{\Tr}{Tr} \DeclareMathOperator{\order}{O} \DeclareMathOperator{\diag}{diag} \DeclareMathOperator{\sgn}{sgn} \DeclareMathOperator{\sech}{sech} $

In this post we collect some information about using colormaps for plotting. In particular, we pay attention to human perception.

In [4]:
!pip install husl
Collecting husl
  Downloading husl-4.0.2.tar.gz
Installing collected packages: husl
  Running setup.py install for husl
Successfully installed husl-4.0.2

Cyclic (phase) Data

In [11]:
%matplotlib inline
from matplotlib import pyplot as plt
import numpy as np
import husl
x = np.linspace(-2, 2, 256)[:, None]
y = x.T
z = x + 1j*y
zs = [0.5, -0.5]
psi = (z-zs[0]) * (z-zs[1])
theta = np.angle(psi)
plt.contourf(theta)
plt.colorbar()
Out[11]:
<matplotlib.colorbar.Colorbar instance at 0x1122cb878>
In [49]:
def rgb(theta):
    c = np.asarray(theta)/np.pi/2.0 + 0.5
    res = np.asarray([husl.huslp_to_rgb(_c*100, 100.0, 50.0) 
                      for _c in c.ravel()])
    return res.reshape((3,) + c.shape)
    
res = rgb(theta)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-49-34770a01199e> in <module>()
      6 
      7 res = rgb(theta)
----> 8 plt.imshow(res)

/data/apps/anaconda/1.3.1/lib/python2.7/site-packages/matplotlib/pyplot.pyc in imshow(X, cmap, norm, aspect, interpolation, alpha, vmin, vmax, origin, extent, shape, filternorm, filterrad, imlim, resample, url, hold, **kwargs)
   2959                         vmax=vmax, origin=origin, extent=extent, shape=shape,
   2960                         filternorm=filternorm, filterrad=filterrad,
-> 2961                         imlim=imlim, resample=resample, url=url, **kwargs)
   2962         draw_if_interactive()
   2963     finally:

/data/apps/anaconda/1.3.1/lib/python2.7/site-packages/matplotlib/axes/_axes.pyc in imshow(self, X, cmap, norm, aspect, interpolation, alpha, vmin, vmax, origin, extent, shape, filternorm, filterrad, imlim, resample, url, **kwargs)
   4642                        filterrad=filterrad, resample=resample, **kwargs)
   4643 
-> 4644         im.set_data(X)
   4645         im.set_alpha(alpha)
   4646         if im.get_clip_path() is None:

/data/apps/anaconda/1.3.1/lib/python2.7/site-packages/matplotlib/image.pyc in set_data(self, A)
    436         if (self._A.ndim not in (2, 3) or
    437             (self._A.ndim == 3 and self._A.shape[-1] not in (3, 4))):
--> 438             raise TypeError("Invalid dimensions for image data")
    439 
    440         self._imcache = None

TypeError: Invalid dimensions for image data
In [52]:
plt.imshow?
In [46]:
 
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-46-40d5d021f12b> in <module>()
----> 1 np.array(res, dtype=float)

ValueError: setting an array element with a sequence.
In [7]:
from matplotlib.colors import Colormap

class HUSLP(Colormap):
    def __init__(self, N=256):
        Colormap.__init__(self, name='huslp', N=N)
    def __call__(self, X, alpha=None, bytes=False):
       """
        Parameters
        ----------
        X : scalar, ndarray
            The data value(s) to convert to RGBA.
            For floats, X should be in the interval ``[0.0, 1.0]`` to
            return the RGBA values ``X*100`` percent along the Colormap line.
            For integers, X should be in the interval ``[0, Colormap.N)`` to
            return RGBA values *indexed* from the Colormap with index ``X``.
        alpha : float, None
            Alpha must be a scalar between 0 and 1, or None.
        bytes : bool
            If False (default), the returned RGBA values will be floats in the
            interval ``[0, 1]`` otherwise they will be uint8s in the interval
            ``[0, 255]``.

        Returns
        -------
        Tuple of RGBA values if X is scalar, othewise an array of
        RGBA values with a shape of ``X.shape + (4, )``.
        """
In [ ]: