Mapping arbitrary data to color scales (pwkit.colormaps)

pwkit.colormaps – tools to convert arrays of real-valued data to other formats (usually, RGB24) for visualization.

TODO: “heated body” map.

The main interface is the factory_map dictionary from colormap names to factory functions. base_factory_names lists the names of a set of color maps. Additional ones are available with the suffixes “_reverse” and “_sqrt” that apply the relevant transforms.

The factory functions return another function, the “mapper”. Each mapper takes a single argument, an array of values between 0 and 1, and returns the mapped colors. If the input array has shape S, the returned value has a shape (S + (3, )), with mapped[…,0] being the R values, between 0 and 1, etc.

Example:

data = np.array ([<things between 0 and 1>]) mapper = factory_map[‘cubehelix_blue’]() rgb = mapper (data) green_values = rgb[:,1] last_rgb = rgb[-1]

The basic colormap names are:

moreland_bluered

Divergent colormap from intense blue (at 0) to intense red (at 1), passing through white

cubehelix_dagreen

From black to white through rainbow colors

cubehelix_blue

From black to white, with blue hues

pkgw

From black to red, through purplish

black_to_white, black_to_red, black_to_green, black_to_blue

From black to the named colors.

white_to_black, white_to_red, white_to_green, white_to_blue

From white to the named colors.

The mappers can also take keyword arguments, including at least “transform”, which specifies simple transforms that can be applied to the colormaps. These are (in terms of symbolic constants and literal string values):

‘none’ - No transform (the default) ‘reverse’ - x -> 1 - x (reverses the colormap) ‘sqrt’ - x -> sqrt(x)

For each transform other than “none”, factory_map contains an entry with an underscore and the transform name applied (e.g., “pkgw_reverse”) that has that transform applied.

The initial inspiration was an implementation of the ideas in “Diverging Color Maps for Scientific Visualization (Expanded)”, Kenneth Moreland,

https://www.kennethmoreland.com/color-maps/

I’ve realized that I’m not too fond of the white mid-values in these color maps in many cases. So I also added an implementation of the “cube helix” color map, described by D. A. Green in

“A colour scheme for the display of astronomical intensity images” http://adsabs.harvard.edu/abs/2011BASI…39..289G (D. A. Green, 2011 Bull. Ast. Soc. of India, 39 289)

I made up the pkgw map myself (who’d have guessed?).