cmap: Scientific Colormaps for Python

0.7.2 · active · verified Fri Apr 17

cmap provides a collection of scientific colormaps for Python, focusing on perceptual uniformity and colorblind-friendliness. It bundles various colormaps like Crameri, Tol, Viridis, and many others, offering a unified API to access and manipulate them. The library is actively maintained with frequent minor releases, currently at version 0.7.2.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to fetch a colormap using `get_cmap`, convert data to RGBA colors, and integrate the `cmap` colormap with Matplotlib for visualization via the `to_mpl()` method.

import numpy as np
import matplotlib.pyplot as plt
from cmap import get_cmap, Colormap

# Get a colormap by name using the utility function
my_cmap = get_cmap("viridis")

# Or, access namespaced colormaps directly
# my_cmap = Colormap.scientific.viridis

# Colormap objects are callable with normalized data [0, 1] or raw data
data = np.linspace(0, 1, 256) # Example data
colors_rgba = my_cmap(data) # Returns (N, 4) array of RGBA floats

# Convert to a Matplotlib colormap for seamless integration with plotting
mpl_cmap = my_cmap.to_mpl()

# Example usage with Matplotlib
fig, ax = plt.subplots(figsize=(6, 1))
plt.imshow([np.arange(256)], cmap=mpl_cmap, aspect='auto')
plt.colorbar(ax.images[0], orientation='horizontal')
plt.title(f"Example Colormap: {my_cmap.name}")
plt.axis('off')
plt.show()

view raw JSON →