Utilities API¶
The rwthplots.utils module provides four helper functions, all re-exported
from the top-level rwthplots namespace:
import rwthplots
rwthplots.save_figure(...)
rwthplots.context(...)
rwthplots.pick_colors(...)
rwthplots.check_accessibility(...)
Figure export¶
rwthplots.utils.save_figure
¶
Save a matplotlib figure to multiple file formats in one call.
Parameters¶
fig : matplotlib.figure.Figure
path : str or pathlib.Path
Output path without file extension. Parent directories are created
automatically.
formats : sequence of str
File formats to write (e.g. ["pdf", "png", "svg"]).
dpi : int or None
DPI override for raster formats. Ignored for vector formats
(pdf, svg, eps, pgf, ps). If None, the figure's current DPI is used.
**savefig_kwargs
Forwarded to :meth:~matplotlib.figure.Figure.savefig.
Examples¶
save_figure(fig, "results/my_plot", formats=["pdf", "png"], dpi=300)
Source code in src/rwthplots/utils.py
Style context¶
rwthplots.utils.context
¶
Return a :func:matplotlib.pyplot.style.context manager with automatic
rwthplots.styles. prefix expansion.
Short names (without rwthplots.styles.) are expanded automatically:
'rwth-latex'→'rwthplots.styles.rwth-latex''color.blue'→'rwthplots.styles.color.blue''misc.grid'→'rwthplots.styles.misc.grid''size.ieee-column'→'rwthplots.styles.size.ieee-column'
Full names (already starting with 'rwthplots.') are passed through.
Examples¶
with rwthplots.context('rwth-latex', 'color.blue', 'misc.grid'): fig, ax = plt.subplots() ax.plot(x, y)
Source code in src/rwthplots/utils.py
Colour selection¶
rwthplots.utils.pick_colors
¶
Return the n most perceptually distinct RWTH colours.
Uses a greedy farthest-point algorithm in CIELAB space. The first colour is always RWTH blue; each subsequent pick maximises the minimum CIE 1976 delta-E to the already-selected set.
Parameters¶
n : int Number of colours to return (1–13). colorset : str RWTH tint level to sample from.
Returns¶
colors : list of str
'#RRGGBB' hex strings in selection order.
Examples¶
pick_colors(4)
['#00549F', '#FFED00', '#57AB27', '#E30066'] (approximately)¶
Source code in src/rwthplots/utils.py
Accessibility¶
rwthplots.utils.check_accessibility
¶
check_accessibility(colors: Sequence[str], types: Sequence[str] = ('deuteranopia', 'protanopia', 'tritanopia'), threshold: float = 10.0) -> list[dict]
Simulate colour vision deficiencies and report pairs that would be confused.
CVD simulation uses the Viénot-Brettel-Mollon model (1997/1999). Perceptual distance is CIE 1976 delta-E in CIELAB.
Parameters¶
colors : sequence of str
'#RRGGBB' hex colour strings to evaluate.
types : sequence of str
CVD types to simulate. Supported: 'deuteranopia',
'protanopia', 'tritanopia'.
threshold : float
Delta-E below which two simulated colours are flagged as confusable.
Default 10 (clearly noticeable); use 20 for a stricter check.
Returns¶
issues : list of dict
Each entry: {'cvd_type', 'color_a', 'color_b', 'delta_e'}.
Empty list means the palette passes.
Examples¶
from rwthplots.cmap import rwth_cset issues = check_accessibility(list(rwth_cset('rwth_100'))) for issue in issues: print(issue)
Source code in src/rwthplots/utils.py
CVD simulation background¶
check_accessibility() uses the Viénot-Brettel-Mollon model (1997/1999)
to simulate how a palette appears to observers with colour vision deficiency.
The simulation pipeline for each colour:
- sRGB → linear RGB — remove gamma correction (IEC 61966-2-1).
- Linear RGB → LMS — transform to the three cone types using the Hunt-Pointer-Estévez / Bradford D65 adaptation matrix.
- Apply CVD confusion matrix — collapse the affected cone channel(s) to reproduce the confusion experienced by each CVD type.
- LMS → linear RGB — invert the cone-space transform.
- Linear RGB → CIELAB — convert to a perceptually uniform space.
- CIE 1976 ΔE — measure Euclidean distance between simulated pairs. Pairs below the threshold are flagged as confusable.
The confusion matrices and sRGB constants are taken from the daltonize project (MIT license), which implements the same model.
| CVD type | Cone affected | Prevalence (males) |
|---|---|---|
| Deuteranopia | M (medium) | ~6 % |
| Protanopia | L (long) | ~2 % |
| Tritanopia | S (short) | ~0.003 % |