cmap0.4.0
Published
Scientific colormaps for python, without dependencies
pip install cmap
Package Downloads
Authors
Project URLs
Requires Python
>=3.8
Dependencies
- numpy
- bokeh
; extra == "dev"
- colorspacious
; extra == "dev"
- colour
; extra == "dev"
- ipython
; extra == "dev"
- matplotlib
; extra == "dev"
- mypy
; extra == "dev"
- napari
>=0.4.19; python_version < "3.13" and extra == "dev"
- numba
; python_version < "3.13" and extra == "dev"
- pdbpp
; extra == "dev"
- plotly
; extra == "dev"
- pre-commit
; extra == "dev"
- pydantic
; extra == "dev"
- pydantic-extra-types
>=2; extra == "dev"
- pygfx
; extra == "dev"
- pyqtgraph
; extra == "dev"
- pytest
; extra == "dev"
- pytest-cov
; extra == "dev"
- pytest-qt
; extra == "dev"
- pytest
>=6.0; extra == "dev"
- qtpy
; extra == "dev"
- rich
; extra == "dev"
- ruff
; extra == "dev"
- viscm
; python_version < "3.13" and extra == "dev"
- vispy
>=0.14; extra == "dev"
- colorcet
; extra == "docs"
- colorspacious
; extra == "docs"
- imageio
; extra == "docs"
- mkdocs
; extra == "docs"
- mkdocs-gen-files
; extra == "docs"
- mkdocs-literate-nav
; extra == "docs"
- mkdocs-material
; extra == "docs"
- mkdocs-minify-plugin
; extra == "docs"
- mkdocstrings-python
; extra == "docs"
- natsort
; extra == "docs"
- pytest-cov
; extra == "test"
- pytest
>=6.0; extra == "test"
- pytest
>=6.0; extra == "test-min"
- bokeh
; extra == "test-thirdparty"
- colorspacious
; extra == "test-thirdparty"
- colour
; extra == "test-thirdparty"
- matplotlib
; extra == "test-thirdparty"
- napari
>=0.4.19; python_version < "3.13" and extra == "test-thirdparty"
- numba
; python_version < "3.13" and extra == "test-thirdparty"
- plotly
; extra == "test-thirdparty"
- pydantic
; extra == "test-thirdparty"
- pydantic-extra-types
>=2; extra == "test-thirdparty"
- pygfx
; extra == "test-thirdparty"
- pyqtgraph
; extra == "test-thirdparty"
- pytest-cov
; extra == "test-thirdparty"
- pytest-qt
; extra == "test-thirdparty"
- pytest
>=6.0; extra == "test-thirdparty"
- qtpy
; extra == "test-thirdparty"
- rich
; extra == "test-thirdparty"
- viscm
; python_version < "3.13" and extra == "test-thirdparty"
- vispy
>=0.14; extra == "test-thirdparty"
cmap
Scientific colormaps for python, with no dependencies beyond numpy.
With cmap
, you can use any of the colormaps from
matplotlib,
cmocean,
colorbrewer,
crameri,
seaborn, and a host
of other collections in your python code, without having to install matplotlib
or any other dependencies beyond numpy.
:book: See the complete catalog
There are a number of python libraries that provide or require colormaps or basic color support, but they all either depend on matplotlib, provide a specialized set of colormaps intended to extend those provided by matplotlib, or roll their own colormap solution that vendors/duplicates other libraries.
cmap
is a lightweight library that provides a large collection of colormaps
with no dependencies beyond numpy. It provides exports to a number of known
third-party colormap objects, allowing it to be used across a wide range of
python visualization libraries. The intention is to provide a library that can
be used by any python library that needs colormaps, without forcing the user to
install matplotlib (while still being compatible with matplotlib and other
libraries that use matplotlib colormaps).
cmap
is strictly typed and fully tested, with a focus on good developer
experience.
Install
pip install cmap
conda install -c conda-forge cmap
Usage
See Documentation for full details.
cmap.Color
The cmap.Color
object is a simple wrapper around a tuple of RGBA scalars, with
a few convenience methods for converting to other color objects.
from cmap import Color
red = Color("red") # or a variety of other "color like" inputs
cmap.Colormap
The cmap.Colormap
object is a callable that can map a scalar value (or numpy
array of values) to an RGBA color (or a numpy array of RGBA colors). API is
intended to mimic the behavior of a
matplotlib.colors.Colormap
object (without requiring matplotlib)
In [1]: import cmap
# or a variety of other "colormap like" inputs
In [2]: cmap1 = cmap.Colormap(["red", "green", "blue"])
In [3]: cmap1(np.linspace(0,1,5))
Out[3]:
array([[1. , 0. , 0. , 1. ],
[0.50393701, 0.24900417, 0. , 1. ],
[0. , 0.50196078, 0. , 1. ],
[0. , 0.24900417, 0.50393701, 1. ],
[0. , 0. , 1. , 1. ]])
Note that the input array must be normalized from 0-1, so if you're applying a colormap
to an integer array (like an image) you must apply any contrast limits and rescale to
0-1 before passing it to a Colormap
.
Third Party Library Support
The cmap.Colormap
object has convenience methods that export it to a number of known
third-party colormap objects, including:
See documentation for details.
If you would like to see support added for a particular library, please open an issue or PR.
Alternatives
Other libraries providing colormaps:
- matplotlib
- seaborn (subclasses matplotlib)
- proplot (subclasses matplotlib)
- palettable (mostly data, import doesn't depend on matplotlib, but usage largely does)
- cmocean (mostly data, outputs matplotlib colormaps)
- colorcet (mostly data, usage requires either matplotlib or bokeh)
- cmasher (requires matplotlib)
- cmyt (requires matplotlib)
- cmcrameri (requires matplotlib, wraps https://www.fabiocrameri.ch/colourmaps/)
- distinctipy (generates distinct color sets, only requires numpy)
- Farrow & Ball Matplotlib (requires matplotlib)
- mplcyberpunk (requires matplotlib)
References and Further reading
- Choosing Colormaps in Matplotlib
- A Better Default Colormap for Matplotlib | SciPy 2015 | Nathaniel Smith and Stéfan van der Walt
- blog post for above video: https://bids.github.io/colormap/
- Origins of Colormaps, Cleve Moler, February 2, 2015
- Documenting the matplotlib colormaps, @endolith
- Color Map Advice for Scientific Visualization
- https://colorcet.com/, Peter Kovesi
- Kovesi: Good Colour Maps: How to Design Them.
- https://www.fabiocrameri.ch/colourmaps/