Skip to content

colors

Colorschemes

Palette

Bases: list[ColorType]

Color palette based on a list of values.

Source code in src/jetplot/colors.py
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
class Palette(list[ColorType]):
    """Color palette based on a list of values."""

    @property
    def hex(self) -> "Palette":
        """Return the palette colors as hexadecimal strings."""
        return Palette([to_hex(rgb) for rgb in self])  # pyrefly: ignore

    @property
    def cmap(self) -> LinearSegmentedColormap:
        """Return the palette as a Matplotlib colormap."""
        return LinearSegmentedColormap.from_list("", self)

    def plot(self, figsize: tuple[int, int] = (5, 1)) -> tuple[Figure, list[Axes]]:
        """Visualize the colors in the palette."""
        fig, axs = plt.subplots(1, len(self), figsize=figsize)
        for c, ax in zip(self, axs, strict=True):  # pyrefly: ignore
            ax.set_facecolor(c)
            ax.set_aspect("equal")
            noticks(ax=ax)

        return fig, cast(list[Axes], axs)

cmap: LinearSegmentedColormap property

Return the palette as a Matplotlib colormap.

hex: Palette property

Return the palette colors as hexadecimal strings.

plot(figsize: tuple[int, int] = (5, 1)) -> tuple[Figure, list[Axes]]

Visualize the colors in the palette.

Source code in src/jetplot/colors.py
31
32
33
34
35
36
37
38
39
def plot(self, figsize: tuple[int, int] = (5, 1)) -> tuple[Figure, list[Axes]]:
    """Visualize the colors in the palette."""
    fig, axs = plt.subplots(1, len(self), figsize=figsize)
    for c, ax in zip(self, axs, strict=True):  # pyrefly: ignore
        ax.set_facecolor(c)
        ax.set_aspect("equal")
        noticks(ax=ax)

    return fig, cast(list[Axes], axs)

cmap_colors(cmap: str, n: int, vmin: float = 0.0, vmax: float = 1.0) -> Palette

Extract n colors from a Matplotlib colormap.

Source code in src/jetplot/colors.py
64
65
66
67
68
69
70
71
def cmap_colors(
    cmap: str,
    n: int,
    vmin: float = 0.0,
    vmax: float = 1.0,
) -> Palette:
    """Extract ``n`` colors from a Matplotlib colormap."""
    return Palette(getattr(cm, cmap)(np.linspace(vmin, vmax, n)))

cubehelix(n: int, vmin: float = 0.85, vmax: float = 0.15, gamma: float = 1.0, start: float = 0.0, rot: float = 0.4, hue: float = 0.8) -> Palette

Cubehelix parameterized colormap.

Source code in src/jetplot/colors.py
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
def cubehelix(
    n: int,
    vmin: float = 0.85,
    vmax: float = 0.15,
    gamma: float = 1.0,
    start: float = 0.0,
    rot: float = 0.4,
    hue: float = 0.8,
) -> Palette:
    """Cubehelix parameterized colormap."""
    lambda_ = np.linspace(vmin, vmax, n)
    x = lambda_**gamma
    phi = 2 * np.pi * (start / 3 + rot * lambda_)

    alpha = 0.5 * hue * x * (1.0 - x)  # pyrefly: ignore
    A = np.array([[-0.14861, 1.78277], [-0.29227, -0.90649], [1.97294, 0.0]])
    b = np.stack([np.cos(phi), np.sin(phi)])

    colors: list[tuple[float, float, float]] = (x + alpha * (A @ b)).T.tolist()
    return Palette(colors)

rainbow(k: int) -> Palette

Return a palette of distinct colors from several base palettes.

Source code in src/jetplot/colors.py
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
def rainbow(k: int) -> Palette:
    """Return a palette of distinct colors from several base palettes."""

    _colors = (
        blue,
        orange,
        green,
        red,
        purple,
        teal,
        pink,
        indigo,
        emerald,
        rose,
        lime,
        sky,
        amber,
    )
    return Palette([r[k] for r in _colors])