A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://cmap-docs.readthedocs.io/en/latest/colormaps/ below:

Colormaps - cmap

Colormaps#

The cmap.Colormap type represents a colormap.

Also known as a LUT (look-up table), a colormap is a mapping from a scalar value to a color. Colormaps are used in a variety of contexts, including image processing, data visualization, and scientific visualization. The cmap library provides a number of built-in colormaps, including all of the colormaps in matplotlib, cmocean, colorbrewer, colorcet, crameri, and more.

from cmap import Colormap

# argument can be any "ColormapLike".  See rules below
cm = Colormap('viridis')
ColormapLike objects#

The following objects can be interpreted as a colormap, and used as the first argument to the cmap.Colormap constructor; cmap refers to these objects collectively as "ColormapLike". Briefly, valid arguments are of type:

str# Iterable[ColorLike | tuple]# numpy.ndarray#

A numpy.ndarray, in one of the following formats:

dict# Callable# Usage# Using qualitative colormaps#

Consider a ColorBrewer qualitative colormap from the default catalog with eight color stops

c = Colormap("colorbrewer:set1_8")
c.color_stops

As with all colormaps, the color stop positions are in [0, 1]

ColorStops(
  (0.0, Color((0.8941, 0.102, 0.1098))),
  (0.14285714285714285, Color((0.2157, 0.4941, 0.7216))),
  (0.2857142857142857, Color((0.302, 0.6863, 0.2902))),
  (0.42857142857142855, Color((0.5961, 0.3059, 0.6392))),
  (0.5714285714285714, Color((1.0, 0.498, 0.0))),
  (0.7142857142857142, Color((1.0, 1.0, 0.2))),
  (0.8571428571428571, Color((0.651, 0.3373, 0.1569))),
  (1.0, Color((0.9686, 0.5059, 0.749)))
)

so a floating point value in [0, 1] can be used to map to a color

which will use nearest neighbor interpolation by default to return the second color exactly

Color((0.2157, 0.4941, 0.7216))

even though the position is in between the second and third color stops.

However, qualitative colormaps are often used to map integer valued or categorical values to colors. The behavior of calling a Colormap depends on the type of the input. Calling a Colormap with an integer

indexes directly into the colormap's LUT

Color((0.2157, 0.4941, 0.7216))

which is often a more natural operation.

Cycling through colors#

When using qualitative colormaps to map integer values, sometimes the input domain may be larger than the number of colors in the colormap.

By default, values less than zero

map to the first color

Color((0.8941, 0.102, 0.1098))

and values greater than the number of colors

map to the last color

Color((0.9686, 0.5059, 0.749))

This behavior can be customized by providing the under and over colors when initializing a Colormap. Instead, sometimes it is preferable for the mapping to cycle through the color stops.

There is currently no built-in way to do this when calling the Colormap, but it can be done by using the modulo operator on the input value with Colormap.num_colors

which now maps to the first color

Color((0.8941, 0.102, 0.1098))

This also works well when using an array as input

c(np.arange(16) % c.num_colors)

which returns the cycled RGBA color values in an array output

array([[0.89411765, 0.10196078, 0.10980392, 1.        ],
       [0.21568627, 0.49411765, 0.72156863, 1.        ],
       [0.30196078, 0.68627451, 0.29019608, 1.        ],
       [0.59607843, 0.30588235, 0.63921569, 1.        ],
       [1.        , 0.49803922, 0.        , 1.        ],
       [1.        , 1.        , 0.2       , 1.        ],
       [0.65098039, 0.3372549 , 0.15686275, 1.        ],
       [0.96862745, 0.50588235, 0.74901961, 1.        ],
       [0.89411765, 0.10196078, 0.10980392, 1.        ],
       [0.21568627, 0.49411765, 0.72156863, 1.        ],
       [0.30196078, 0.68627451, 0.29019608, 1.        ],
       [0.59607843, 0.30588235, 0.63921569, 1.        ],
       [1.        , 0.49803922, 0.        , 1.        ],
       [1.        , 1.        , 0.2       , 1.        ],
       [0.65098039, 0.3372549 , 0.15686275, 1.        ],
       [0.96862745, 0.50588235, 0.74901961, 1.        ]])

The behavior of calling a Colormap with an array depends on its dtype. With a floating point dtype, it expects the values to be [0, 1], so the equivalent call to the above is

c(np.linspace(0, 2, 16, endpoint=False) % 1)

which returns the same output array values.

Immutability#

All colormaps are immutable and cannot be modified after instantiation.

Usage with external visualization libraries#

A primary motivation of cmap is to make it easy to use colormaps in external visualization libraries. To that end, cmap.Colormap provides to_<libname>() methods for a number of libraries:

Tip

Some of these methods take additional arguments, see Colormap API for details.

Usage with pydantic#

Colormap can be used as a field type in pydantic. models.

from pydantic import BaseModel
from cmap import Colormap

class Foo(BaseModel):
    colormap: Colormap


foo = Foo(colormap='viridis')  # or any other ColormapLike
foo.colormap

Serialization in pydantic

Unfortunately, serialization with the json module is not easily pluggable, so if you want to serialize a pydantic model with a Colormap field to JSON, add the following encoder to your model:

class Foo(BaseModel):
    colormap: Colormap

    class Config:
        json_encoders = {Colormap: Colormap.as_dict}

#example
Foo(colormap=['red', 'green']).json()

results in

{"colormap":
    {"name": "custom colormap",
     "identifier": "custom_colormap",
      "category": null,
      "color_stops": [[0.0, [1.0, 0.0, 0.0, 1]], [1.0, [0.0, 0.5019607843137255, 0.0, 1]]]
    }
}

Serialization in psygnal.EventedModel

Colormap supports serialization in psygnal.EventedModel out of the box. The json_encoders = {Colormap: Colormap.as_dict} line in the Config class mentioned above is not necessary.

Rich repr#

If you use rich pretty printing, Colormap objects have a nice repr that shows the color in the terminal

from rich import pretty

pretty.install()


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4