Skip to main content

C3 for Python - Consistent Categorical Colors

A pure Python port of @mapequation/c3: deterministic, distinct colors for categories and nested modules, spread over hue, lightness and chroma in OKLCH. No dependencies.

It gives the same colors as the npm package, checked against shared fixtures (within rounding), and the version numbers match, so c3-colors 2.0.0 gives the colors of @mapequation/c3 2.0.0.

Install

pip install c3-colors

or uv add c3-colors, and import it as c3. Requires Python 3.11 or later.

Usage

The API mirrors the JavaScript one, with options as keyword arguments in snake_case. The tutorial notebook walks through it with matplotlib, and colors a map with geopandas and network modules with networkx and Infomap.

import c3

c3.palette(5)
# ['#2b9bea', '#cf9f71', '#3b8559', '#b46ba5', '#00b3bb']

c3.palette(10, tone="pastel", offset=90)
c3.palette([3, 1, 1, 2])  # weights: heavier colors get more hue space
c3.palette(8, seeds=["#ff0000"], method="farthest")
c3.palette(40, end="darker")  # later colors in a darker end zone
c3.palette(12, tone={"chroma": (0.1, 0.2), "warm_hue": 60, "tilt": 0.3})
c3.palette(10, tone=c3.swatches["mural"])  # infer the tone from colors

colors = c3.hierarchy([
    {"path": [1, 1], "flow": 0.3},
    {"path": [1, 2], "flow": 0.2},
    {"path": [2, 1], "flow": 0.5},
])
colors([1, 2])  # color of module 1:2
colors.modules  # all modules with path, flow, color and radius, depth first

blues = c3.sequential(seeds=["#3b82f6"])  # through a color, extended to darker and lighter shades
blues(0.5)  # '#3c83f6', from light at 0 to dark at 1
blues.colors(7)  # 7 steps, '#3b82f6' among them
c3.sequential(seeds=["#2a9d8f", "#e9c46a"]).colors(9)
c3.sequential(seeds=["#d26202"], light_hue=40, dark_hue=-15).colors(9)  # lighter shades turn yellow, darker red
c3.sequential(offset=30).colors(9)  # through palette(offset=30)'s first color
c3.sequential(seeds=["#fff5eb", "#7f2704"], path="straight").colors(9)  # paler oranges than the default

c3.tones["vivid"]  # {'lightness': (0.55, 0.7), 'chroma': (0.15, 0.21), 'warm_hue': 285, 'tilt': 0}
c3.infer_tone(["#e41a1c", "#377eb8", "#4daf4a"])
c3.oklab("steelblue"), c3.oklch("#2b9bea")
c3.distance("#2b9bea", "#2b9beb")  # about 0.02 is just noticeable

Tone and end zone fields are snake_case too: warm_hue for warmHue, min_gap for minGap. A Sequential scale also works as a hue source, and matplotlib.colors.ListedColormap(scale.colors(256)) turns it into a colormap.

Hue sources

hue is "Oklch" (the OKLCH hue circle, the default), the name of a d3 color scheme, or any callable from t in [0, 1] to a color. The names are the ones the npm package accepts, with the same colors: "Viridis", "Cividis", "CubehelixDefault", "Turbo", "Rainbow" and "Sinebow", and the ColorBrewer schemes, sequential ("Blues", "Greys", "YlGnBu", …) and diverging ("RdBu", "Spectral", …). The callable can return a CSS color string like "#rrggbb" or "rgb(r, g, b)", or a sequence of floats in [0, 1] (alpha is ignored), the convention of matplotlib colormaps:

c3.palette(10, hue="Viridis")
c3.sequential(hue="Viridis").colors(9)  # viridis from light to dark
c3.sequential(hue="Viridis", lightness=(0.55, 0.97), light_chroma=0.6, dark_chroma=0.6)  # lighter, softer

import matplotlib

c3.sequential(hue=matplotlib.colormaps["magma"]).colors(9)  # any colormap

In palette() and hierarchy() the scheme gives only the hues. sequential() without seeds follows the whole scheme instead: its own colors by default, from light to dark, stretched to lightness when you give it, with light_chroma, dark_chroma, light_hue and dark_hue scaling its chroma and turning its hue evenly from end to end. The scheme's lightness must go one way, so "Turbo", "Sinebow", "Rainbow" and diverging schemes like "RdBu" raise a ValueError there.

matplotlib is not a dependency. A ColorBrewer scheme by name can differ slightly from the matplotlib colormap of the same name: like d3, c3 runs a B-spline through ColorBrewer's colors, where matplotlib interpolates them linearly.

Development

cd python
uv run pytest
uv run --group notebook python examples/execute.py   # re-execute the tutorial in place

The tests check every case in fixtures/c3.json at the repository root, which the TypeScript library generates with pnpm --filter @mapequation/c3 fixtures. They also check that the committed tutorial notebook has been executed without errors, so re-execute it after changing colors or the notebook. The notebook dependency group has what the tutorial needs, and the notebook downloads the Census state boundaries when it runs.

Metadata

Release files for c3-colors 2.2.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 c3-colors 2.2.0
File Size Uploaded Provenance Actions
c3_colors-2.2.0.tar.gz 29.6 kB With provenance Details for c3_colors-2.2.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for c3-colors 2.2.0
File Interpreter ABI Platform Actions
c3_colors-2.2.0-py3-none-any.whl Python 3 none any Details for c3_colors-2.2.0-py3-none-any.whl

Total release size: 63.8 kB

Release files / c3_colors-2.2.0.tar.gz

Download URL c3_colors-2.2.0.tar.gz
Size 29.6 kB
Tags Source
SHA-256 checksum
How to use checksums
2811c9d4ed85bdb727e716d4c5f8347ee7eb67376e6884c44bd324503b74e35c
BLAKE2b-256 checksum
How to use checksums
f2bc6ce2b6973811f14eeb9127c321b198e0dde430a2e135c85d04c16306fb2c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

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 21, 2026.

Transparency log

Release files / c3_colors-2.2.0-py3-none-any.whl

Download URL c3_colors-2.2.0-py3-none-any.whl
Size 34.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
485db3a736d95431450fa7720bdd88a97a55df3db531c6f84c3593c3f14729db
BLAKE2b-256 checksum
How to use checksums
ac94ca77f60afe37902892416bfe28f2b40befd8db716191cefb6b15b0e8d3b1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

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 21, 2026.

Transparency log

Security for c3-colors 2.2.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 c3_colors-2.2.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 21, 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