Skip to main content

perfect-cmaps

This package provides Python functionality for creating custom colormaps which are perfectly perceptually uniform. The project came up as a result of there being too few easily accessible perceptually uniform colormaps, so I wanted to have a go at creating a good library where users can easily create their own. I wasn't quite happy with colormaps such as viridis, plasma or magma from matplotlib, so I started making some algorithmic colormaps.

To stay as true as possible to the lightness achieved by a true grayscale colormap, but add more information with colors, the perceptual range of colormaps is optimized to be as high as possible.

Main package functionality

  • create_cmap(): Create your own colormap.
  • get_cmap(): Get a named colormap from the library -- an instance of matplotlib.colors.LinearSegmentedColormap.
  • list_cmaps(): Lists all available local and internal colormaps.
  • compare_cmaps(): Plots a comparison of colormaps by name, or of all available colormaps if no names are given.
  • plot_cmap(): Plots a single colormap (or colormap name) with its lightness profile, a test image and its color channels.
  • optimize_cmap(): Reoptimizes the lightness of an existing colormap, such as one from matplotlib.

Example of creating a custom colormap

run: python ./perfect_cmaps/customize_cmap.py --num_points 21 --lightness linear or a similar command

This specifies that your colormap will have 21 control points, which will later be interpolated for desired granularity, and that the colormap will have a linear lightness profile.

In the window, you choose one color per lightness level:

  • Click to choose a color. Clicks outside the gamut are pulled onto its boundary, keeping the hue.
  • Right-click or Backspace to undo the last color.
  • V to switch between the plain a*b* plane and a view where each gamut slice is stretched to fill the square. The stretched view keeps the hue angle, and the distance from the center is the chroma relative to the most saturated color of that hue, which makes the small slices near black and white easier to work with. Start in it with --view square.
  • Esc to quit without saving.

When all colors are chosen, the final colormap is shown, and you can name it and save it.

https://github.com/user-attachments/assets/83e53b18-62d8-4003-b7ef-ad37042df294

You can also change the view to a square-transformed version of the CIELAB gamut slices, which can make it easier to place control points:

https://github.com/user-attachments/assets/c8f72af1-411e-49bf-8fb5-7bbd9b85efd3

Generated colormap

To test out your colormap, you can use the function get_cmap() or use the main function from colormaps.py:

python ./perfect_cmaps/colormaps.py --colormap test_colormap -n 1000 --interpolation quadratic --smoothing 0.03

import perfect_cmaps as pcm
cmap = pcm.get_cmap("test_colormap", n=1000, interpolation="quadratic", smoothing=0.03)
pcm.plot_cmap(cmap)

Here we specify to use a colormap called test_colormap and create the colormap with 1000 points, with quadratic interpolation and some amount of smoothing applied.

Generated colormap

Optimizing existing colormaps

Colormaps from matplotlib are often not perfectly linear (or diverging) in lightness, and don't always use the full lightness range. optimize_cmap() replaces the L* channel of a colormap with one of the lightness profiles.

import perfect_cmaps as pcm
cmap = pcm.optimize_cmap("viridis", plot=True)                   # profile is detected: linear
cmap = pcm.optimize_cmap("coolwarm", lightness="diverging")       # or choose the profile yourself
cmap = pcm.optimize_cmap("Blues", save_name="blues_optimized")    # save it for get_cmap()
cmap = pcm.get_cmap("blues_optimized")
  • The lightness profile is detected from the colormap's own lightness unless you give lightness.
  • Colormaps that go from light to dark, like Blues or viridis_r, keep their direction.
  • By default, the colors (a* and b*) are kept as they are, and the lightness range stays within the original one. l_dark and l_light work as in get_cmap(). A saved colormap keeps its lightness bounds when loaded with get_cmap().
  • Keeping a* and b* fixed limits how wide the lightness range can be, since every color has to stay within the sRGB gamut. With target_range ("original", "full" or (l_dark, l_light)), colors are moved towards grey, keeping their hue, just enough to reach the given range, and the range is then maximized. Chroma is scaled per control point, so n_points (default 32 with target_range) controls how gradual the change is: fewer points give smoother chroma through interpolation. E.g. pcm.optimize_cmap("magma", target_range="original") makes magma linear while keeping its original lightness range. Use plot=True to see the lightness and chroma before and after.
  • Colormaps whose colors can't all be fitted to the profile within the gamut, like jet or hsv, print a warning.

From the command line: python -m perfect_cmaps.optimize --colormap viridis --target_range full --save viridis_optimized.

Test images

Test images

Here's a compilation of some colormaps generated with this library, compared with some standard colormaps available in matplotlib: Comparison

The top two colormaps, viridis and cividis have a lower perceptual range, and thus look less clear -- almost like there's a filter on them. The colormaps generated with perfect-cmaps are crisper and offer a maximal perceptual range. Additionally, the perceptual limitation for the standard colormaps is especially visible when for example a background in grayscale is very white, in which case the standard colormaps are in this case yellow and clearly do not cover the entire lightness spectrum.

Comparison2

The algorithmically created ectotherm colormap in fact covers 100 % of the perceptual range, while adding more information through colors. It ranges from completely black to completely white, while adding colors in between and maintaining truly linearly increasing lightness. This is one of my favorites! The other two displayed custom colormaps mimic viridis and cividis, but with a higher perceptual range. The standard ectotherm colormap is linear in luminance, whereas the ectotherm_l colormap is linear in lightness. I recommend the ectotherm_l colormap for general use.

Ectotherm gradient Ectotherm examples

You may have heard that standard RGB colormaps should be avoided. Well, common RGB colormaps are indeed not at all perceptually uniform, but if you design them right, you can still have a useful colormap with much color information, perceptual range and true perceptual uniformity.

Spectrum gradient Spectrum examples

Thanks to Nanoscience.com for letting me use the grayscale images as examples! Find them at https://www.nanoscience.com/techniques/scanning-electron-microscopy/.

These custom generated colormaps are theoretically perceptually uniform, but note that some screen settings may show colormap artifacts, making the colormaps appear less perceptually uniform. This may happen at lower computer screen lighting, for example.

Metadata

Release files for perfect-cmaps 2.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for perfect-cmaps 2.1.0
File Size Uploaded Provenance Actions
perfect_cmaps-2.1.0.tar.gz 8.3 MB With provenance Details for perfect_cmaps-2.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for perfect-cmaps 2.1.0
File Interpreter ABI Platform Actions
perfect_cmaps-2.1.0-py3-none-any.whl Python 3 none any Details for perfect_cmaps-2.1.0-py3-none-any.whl

Total release size: 16.5 MB

Release files / perfect_cmaps-2.1.0.tar.gz

Download URL perfect_cmaps-2.1.0.tar.gz
Size 8.3 MB
Tags Source
SHA-256 checksum
How to use checksums
d198fdc7129ce0a852e54d22fcbfd4afba477966fc8e46711f7b2d7bda51a664
BLAKE2b-256 checksum
How to use checksums
9cdd7de5d96f5775fde626154ed0ae8ccd819b7c1438b95d19825498044c8d0e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 12, 2026.

Transparency log

Release files / perfect_cmaps-2.1.0-py3-none-any.whl

Download URL perfect_cmaps-2.1.0-py3-none-any.whl
Size 8.3 MB
Tags Python 3
SHA-256 checksum
How to use checksums
f731e96eae197c02ab03998b5d2dea4240499c5aebcaf4f9776c3962808849e9
BLAKE2b-256 checksum
How to use checksums
0b6ce738b1384f5595441688a6e34322a3b5008ef2047f6cbbbba84574f34d52
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 12, 2026.

Transparency log

Security for perfect-cmaps 2.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for perfect_cmaps-2.1.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 12, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page