Skip to content

apply_colormap

apply_colormap(C, f, min=None, max=None, log=False, piecewise_linear=False)

Maps function values to colors of a given colormap.

Apply a colormap produced with the colormap function to an array of scalar values.

Parameters:

Name Type Description Default
C numpy int array

Colormap matrix where each row corresponds to an RGB color. Compatible with maps produced by colormap

required
f numpy double array

Vector of scalar function values to which to apply the colormap

required
min double, optional (default None)

Scalar value corresponding to the beginning of C. Will auto-scale if None.

None
max double, optional (default None)

Scalar value corresponding to the end of C. Will auto-scale if None.

None
log bool, optional (default False)

Whether to log-scale the colormap or not. For this option, all values will be clamped to larger than machine epsilon.

False
piecewise_linear bool, optional (default False)

Whether to treat the colormap as piecewise linear

False

Returns:

Name Type Description
f_colored numpy double array

Matrix of C applied to f. Each row corresponds to a scalar value in f.

See Also

colormap.

Notes

All colormaps are assumed to be piecewise constant unless otherwise specified (be aware: the first and last colors of the colormap will span half as much length as the other colors, since they represent the colors at the minimum and maximum respectively).

Examples:

TODO

Source code in src/gpytoolbox/apply_colormap.py
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
def apply_colormap(C, f,
       min=None,
       max=None,
       log=False,
       piecewise_linear=False):
    """Maps function values to colors of a given colormap.

    Apply a colormap produced with the colormap function to an array of
    scalar values.

    Parameters
    ----------
    C : numpy int array
        Colormap matrix where each row corresponds to an RGB color. Compatible with maps produced by colormap
    f : numpy double array
        Vector of scalar function values to which to apply the colormap
    min : double, optional (default None)
        Scalar value corresponding to the beginning of C. Will auto-scale if None.
    max : double, optional (default None)
        Scalar value corresponding to the end of C. Will auto-scale if None.
    log : bool, optional (default False)
        Whether to log-scale the colormap or not. For this option, all values will be clamped to larger than machine epsilon.
    piecewise_linear : bool, optional (default False)
        Whether to treat the colormap as piecewise linear

    Returns
    -------
    f_colored : numpy double array
        Matrix of C applied to f. Each row corresponds to a scalar value in f.

    See Also
    --------
    colormap.

    Notes
    -----
    All colormaps are assumed to be piecewise constant unless otherwise
    specified (be aware: the first and last colors of the colormap will span
    half as much length as the other colors, since they represent the colors
    at the minimum and maximum respectively).


    Examples
    --------
    TODO
    """

    assert C.shape[0] >= 1
    assert C.shape[1] == 3

    if C.shape[0] == 1:
        return np.repeat(C, f.shape[0], axis=0)

    interpolation_mode = 'linear' if piecewise_linear else 'nearest'

    minimum = np.amin(f) if min is None else min
    if log and minimum < np.finfo(np.float64).eps:
        minimum = np.finfo(np.float64).eps
    maximum = np.amax(f) if max is None else max
    assert maximum >= minimum
    if maximum < minimum + np.finfo(np.float64).eps:
        maximum = minimum + np.finfo(np.float64).eps

    f = np.minimum(np.maximum(f, minimum), maximum)

    if log:
        xaxis = np.logspace(np.log2(minimum), np.log2(maximum), C.shape[0],
              base=2.)
    else:
        xaxis = np.linspace(minimum, maximum, C.shape[0])

    interpolator = sp.interpolate.interp1d(xaxis, C, axis=0,
        kind=interpolation_mode)
    return interpolator(f).round().astype(int)