plotly_tools.cmap_plt2js

plotly_tools.cmap_plt2js(cmap, cmin=0.0, cmax=1.0, Ncols=None)[source]

Converts Matplotlib colormaps to JavaScript color-scales

Parameters:

cmap : Matplotlib colormap

A linear segmented colormap (e.g., any member of plt.cm).

cmin : float

Lower cutoff value for truncating the input colormap that satisfies 0 <= cmin < 1.

cmax : float

Upper cutoff value for truncating the input colormap that satisfies 0 < cmax <= 1.

Ncols : int

Resolution of output color-scale, e.g., if Ncols = 12, the output color-scale will contain 12 discrete RGB color increments.

Returns:

cscale : list

List of lists containing Ncols float-string pairs encoding value-RGB color assignments in increasing order starting with cscale[0] = [0, ‘rgb(n_1,m_1,l_1)’] up to cscale[Ncols-1] = [1.0, ‘rgb(n_Ncols,m_Ncols,l_Ncols)’], where n_1,...,n_Ncols, m_1,...,m_Ncols and l_1,...,l_Ncols are integers between 0 and 255 (see Examples for details).

See also

None

Notes

None

Examples

The following command converts and down-samples the jet colormap in the range [0.1, 1] to a JavaScript color-scale with 12 components

>>> import matplotlib.pyplot as plt
>>> cscale = cmap_plt2js(plt.cm.jet,cmin=0.1,Ncols=12)
>>> cscale
[[0.0, 'rgb(0,0,241)'],
 [0.090909090909090912, 'rgb(0,56,255)'],
 [0.18181818181818182, 'rgb(0,140,255)'],
 [0.27272727272727271, 'rgb(0,224,251)'],
 [0.36363636363636365, 'rgb(64,255,183)'],
 [0.45454545454545459, 'rgb(131,255,115)'],
 [0.54545454545454541, 'rgb(199,255,48)'],
 [0.63636363636363635, 'rgb(255,222,0)'],
 [0.72727272727272729, 'rgb(255,145,0)'],
 [0.81818181818181823, 'rgb(255,67,0)'],
 [0.90909090909090917, 'rgb(218,0,0)'],
 [1.0, 'rgb(128,0,0)']]