Skip to content

Colors

property_to_color(prop, cmap='viridis', **kwargs)

Convert a scalar array of property values to colors, given a provided color map (or property name).

Parameters:

Name Type Description Default
prop array_like

the scalar array of property values

required
cmap str

the color map name or property name

'viridis'
kwargs dict

optional keyword arguments

{}

Returns:

Type Description
array_like

the array of color values for the given property

Source code in chmpy/util/color.py
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
def property_to_color(prop, cmap="viridis", **kwargs):
    """
    Convert a scalar array of property values to colors, 
    given a provided color map (or property name).

    Args:
        prop (array_like): the scalar array of property values
        cmap (str): the color map name or property name
        kwargs (dict): optional keyword arguments

    Returns:
        array_like: the array of color values for the given property
    """
    from matplotlib.cm import get_cmap

    midpoint = kwargs.get("midpoint", 0.0 if cmap in ("d_norm", "esp") else None)
    colormap = get_cmap(kwargs.get("colormap", DEFAULT_COLORMAPS.get(cmap, cmap)))
    norm = None
    if midpoint is not None:
        try:
            from matplotlib.colors import TwoSlopeNorm
        except ImportError:
            from matplotlib.colors import DivergingNorm as TwoSlopeNorm
        vmin = prop.min()
        vmax = prop.max()
        if vmin >= 0.0:
            vmin = -1.0
        if vmax <= 0.0:
            vmax = 1.0
        norm = TwoSlopeNorm(vmin=vmin, vcenter=midpoint, vmax=vmax)
        prop = norm(prop)
    return colormap(prop)