Palette API¶
Examples assume import hedgehogs as hdg.
API index¶
| API | Purpose | Returns |
|---|---|---|
hdg.Palette(...) |
Hold named, ordered colours | Palette |
hdg.get_palette(...) |
Select a registered palette | Palette |
hdg.build_color_map(...) |
Assign colours to labels | dict |
hdg.build_style_map(...) |
Assign colour, marker and line styles | dict |
hdg.register_palette(...) |
Register validated colours | Palette |
hdg.save_palette(...) |
Write a palette to JSON | Path |
hdg.load_palette(...) |
Read and register JSON colours | Palette |
Built-in palettes¶
Hedgehogs provides six categorical palettes; Okabe–Ito is the default. Swatches follow the exact package order, from left to right. The summary lists HEX values in the same order; indices start at zero.
| Palette | Colours | Preview | HEX values in index order |
|---|---|---|---|
okabe-ito |
8 | #0072B2 #E69F00 #D55E00#009E73 #56B4E9 #F0E442#CC79A7 #000000 |
|
tableau10 |
10 | #4E79A7 #F28E2B #E15759#76B7B2 #59A14F #EDC948#B07AA1 #FF9DA7 #9C755F#BAB0AC |
|
paul-tol-vibrant |
7 | #0077BB #33BBEE #009988#EE7733 #CC3311 #EE3377#BBBBBB |
|
paul-tol-bright |
7 | #4477AA #EE6677 #228833#CCBB44 #66CCEE #AA3377#BBBBBB |
|
paul-tol-muted |
9 | #332288 #88CCEE #44AA99#117733 #999933 #DDCC77#CC6677 #882255 #AA4499 |
|
ibm |
5 | #648FFF #785EF0 #DC267F#FE6100 #FFB000 |
Colour names belong to each palette; the same name can have different HEX values across palettes. These are discrete series colours. Select a Matplotlib colormap separately for continuous fields. Combine colours with markers or line styles for greyscale reproduction.
Select colours¶
palette = hdg.get_palette("okabe-ito")
ax.scatter(x, y, color=palette["blue"])
ax.plot(x, prediction, color=palette[1])
Palette supports colour names, integer indices and slices. Integer indices cycle through the available colours; slices return colour lists. Available names depend on the selected palette. len(palette) reports the colour count; keys(), values() and items() expose names, colours and name–colour pairs. Built-in colour-name lookup ignores case. An unknown colour name raises KeyError.
Apply a palette to the global figure style with hdg.set_style(palette="okabe-ito"). For a pairplot, hdg.plots.pairplot(data, hue="species", palette="tableau10") selects its palette explicitly.
For the default palette, palette[0] and palette["blue"] both return "#0072B2"; palette[8] repeats the first colour. palette[:3] returns a list of the first three colours, while hdg.get_palette("okabe-ito", n=3) returns a three-colour Palette.
Assign series styles¶
colors = hdg.build_color_map(["baseline", "model"])
ax.plot(x, baseline, label="Baseline", color=colors["baseline"])
ax.plot(x, prediction, label="Model", color=colors["model"])
styles = hdg.build_style_map(["baseline", "model"])
ax.plot(x, baseline, label="Baseline", **styles["baseline"])
ax.plot(x, prediction, label="Model", **styles["model"])
build_color_map() assigns colours; build_style_map() also assigns markers and line styles. Labels retain first-occurrence order. Both default to Okabe–Ito independently of the active figure style.
Styles cycle through -, --, -., : and markers through o, s, ^, D, v, P, X. Colours and complete combinations eventually repeat.
Register and save¶
hdg.register_palette("models", {"baseline": "#0072B2", "model": "#D55E00"})
hdg.save_palette("models", "models.json")
hdg.load_palette("models.json", name="imported-models")
Registration lasts for the current Python process; JSON transfers palettes between sessions. See Custom palettes for validation and file naming.
Palette objects¶
Palette¶
hdg.Palette(names, colors)
names and colors are ordered iterables. See Select colours for access and iteration. Use register_palette() to construct validated custom palettes.
palette = hdg.Palette(["baseline", "model"], ["#0072B2", "#D55E00"])
ax.plot(x, baseline, color=palette["baseline"])
get_palette¶
Read a registered palette or select colours from an existing Palette.
hdg.get_palette(palette="okabe-ito", n=None) -> Palette
Returns a Palette; see Built-in palettes for names and colour order.
| Parameter | Default | Meaning |
|---|---|---|
palette |
"okabe-ito" |
Registered name or Palette object |
n |
None |
Positive integer limiting the number of colours; None retains all colours |
An invalid n raises ValueError. A limit larger than the palette returns all available colours. Palette names ignore case and accept partial family names such as tol or tableau; an unrecognised name emits UserWarning and falls back to Okabe–Ito. Prefer the exact names in the tables for reproducible selection.
Series mapping¶
build_color_map and build_style_map¶
Assign colours or combined visual styles to unique labels.
hdg.build_color_map(labels, palette="okabe-ito") -> dict[object, str]
hdg.build_style_map(labels, palette="okabe-ito") -> dict[object, dict[str, str]]
labels contains hashable labels; palette accepts a registered name or Palette. build_style_map() returns keyword dictionaries for Axes.plot(). See Assign series styles for ordering and cycles.
Custom palettes¶
Register palettes for the current process or transfer them through JSON.
hdg.register_palette(name, colors, names=None, *, overwrite=False) -> Palette
hdg.save_palette(name, path) -> Path
hdg.load_palette(path, *, name=None, overwrite=False) -> Palette
colors accepts an ordered mapping of names to Matplotlib colours, or an iterable with optional names. Omit names for mappings; unnamed iterables receive color-1, color-2, and so on. Prefer lowercase names because lookup lowercases its input. Empty palettes, invalid colours, duplicate names and mismatched lengths raise ValueError. Replacing a registration requires overwrite=True.
save_palette() writes name, names and colors to JSON and returns its path. load_palette() registers the file and returns a Palette; its name comes from an explicit name, the JSON name or the file stem, in that order.