Accessing the colormaps¶
cc, all the colormaps shown in this notebook will be available for use in different forms. It's a bit difficult to describe, but the idea is that colorcet should have at least one such form convenient for any particular application. There are two different basic versions for each colormap, each of which is fundamentally a list of 256 distinct colors:
- A Bokeh-style palette, i.e., a Python list of RGB colors as hex strings, like
['#000000', ..., '#ffffff']
- If matplotlib is installed and importable, a Matplotlib
LinearSegmentedColormapusing normalized magnitudes, like
LinearSegmentedColormap.from_list("fire",[ [0.0,0.0,0.0], ..., [1.0,1.0,1.0] ], 256)
The Bokeh-compatible palettes are provided as attributes in the
colorcet namespace, with long names prefixed with
b_ or a short name (if any) with no prefix. E.g.
linear_kryw_0_100_c71 can be accessed as
cc.b_linear_kryw_0_100_c71 or by the short name
cc.fire, which both return the same Bokeh palette. These names should tab complete once
cc has been imported. Because Bokeh palettes are just Python lists, you can always reverse them using normal Python syntax, e.g.
list(reversed(cc.fire)), or use subsets of them with slice notation, e.g.
cc.fire[25:]. If you want to access the palettes by string name, they are also collected into a dictionary named
palette, so you can use
cc.palette.fire; whichever is more convenient. Finally, the subset of colormaps that have short, readable names are available separately, accessible as
cc.palette_n["fire"], e.g. for use in GUI widgets selecting a colormap by readable name.
The Matplotlib colormaps are also provided as tab-completable attributes, but consistently with a prefix
cc.m_fire. Already reversed versions are also available, as
cc.m_fire_r. The same colormaps are also registered with matplotlib's string-based dictionary with the prefix
cet_, making them available by name within various matplotlib functions (e.g.
cet_fire_r). Finally, if you want to access the colormaps by string name without using Matplotlib's registry, they are also stored in the
cc.cm dictionary, e.g.
In each case, the colormap names used are the shortest ones that are expected to be unique in that context, and in practice you are only likely to need one of these forms for any particular application.
import colorcet as cc cc.fire[:5]
['#000000', '#060000', '#0d0000', '#120000', '#160000']
For ease of use, we also provide minimal plotting commands for use with colorcet. These depend on holoviews, which needs to be installed before they can be used. Once set up, these commands provide easy viewing capability of the colormaps.
colorcet.plotting and load your desired backend into holoviews. Then call
swatch with the name of a colormap.
from colorcet.plotting import swatch, swatches import holoviews as hv hv.extension('matplotlib')