Skip to content

Normalizations

A Normalize maps data values into [0, 1] for colormap lookup — the color-axis counterpart of a Scale. Pass one as norm= to imshow, a colormapped scatter, or the field marks; the colorbar follows it.

norms

Normalizations: map data values into [0, 1] for colormap lookup.

A Normalize (and its subclasses) is the colorbar/colormap analog of a Scale: norm(value) returns the position in [0, 1] a value occupies on the color axis, and norm.colorbar_ticks() locates labeled ticks for a colorbar. Used by Axes.scatter (c=) and Axes.imshow (norm=).

Normalize

Normalize(
    vmin: float | None = None, vmax: float | None = None
)

Linear normalization between vmin and vmax (clamped to [0, 1]). vmin/vmax left None are filled from the data by autoscale.

Source code in python/pyplotrs/norms.py
def __init__(self, vmin: float | None = None, vmax: float | None = None) -> None:
    self.vmin = None if vmin is None else float(vmin)
    self.vmax = None if vmax is None else float(vmax)

autoscale

autoscale(values: Sequence[float]) -> 'Normalize'

Fill any unset vmin/vmax from the finite members of values.

Source code in python/pyplotrs/norms.py
def autoscale(self, values: Sequence[float]) -> "Normalize":
    """Fill any unset ``vmin``/``vmax`` from the finite members of ``values``."""
    if self.vmin is None or self.vmax is None:
        lo, hi = _core.data_range(_as_f64(values)) or (0.0, 1.0)
        if self.vmin is None:
            self.vmin = lo
        if self.vmax is None:
            self.vmax = hi
    if self.vmin == self.vmax:  # avoid a zero-width range
        self.vmax = self.vmin + 1.0
    return self

LogNorm

LogNorm(
    vmin: float | None = None, vmax: float | None = None
)

Bases: Normalize

Logarithmic normalization (positive data). Colorbar ticks fall on decades.

Source code in python/pyplotrs/norms.py
def __init__(self, vmin: float | None = None, vmax: float | None = None) -> None:
    self.vmin = None if vmin is None else float(vmin)
    self.vmax = None if vmax is None else float(vmax)

TwoSlopeNorm

TwoSlopeNorm(
    vcenter: float,
    vmin: float | None = None,
    vmax: float | None = None,
)

Bases: Normalize

Diverging normalization: vcenter maps to 0.5 with independent slopes on each side (for asymmetric data around a meaningful midpoint).

Source code in python/pyplotrs/norms.py
def __init__(self, vcenter: float, vmin: float | None = None,
             vmax: float | None = None) -> None:
    super().__init__(vmin, vmax)
    self.vcenter = float(vcenter)

BoundaryNorm

BoundaryNorm(boundaries: Sequence[float])

Bases: Normalize

Map values into discrete bins defined by boundaries (monotone), each bin getting an evenly-spaced color position (for stepped colorbars).

Source code in python/pyplotrs/norms.py
def __init__(self, boundaries: Sequence[float]) -> None:
    bnd = [float(b) for b in boundaries]
    super().__init__(bnd[0], bnd[-1])
    self.boundaries = bnd
    self.nbins = len(bnd) - 1

get

get(
    norm, vmin: float | None, vmax: float | None
) -> Normalize

Resolve a norm argument (a Normalize, the string "log", or None) plus optional vmin/vmax into a Normalize.

Source code in python/pyplotrs/norms.py
def get(norm, vmin: float | None, vmax: float | None) -> Normalize:
    """Resolve a ``norm`` argument (a ``Normalize``, the string ``"log"``,
    or ``None``) plus optional ``vmin``/``vmax`` into a ``Normalize``."""
    if norm is None:
        return Normalize(vmin, vmax)
    if isinstance(norm, Normalize):
        if vmin is not None and norm.vmin is None:
            norm.vmin = float(vmin)
        if vmax is not None and norm.vmax is None:
            norm.vmax = float(vmax)
        return norm
    if isinstance(norm, str):
        if norm == "log":
            return LogNorm(vmin, vmax)
        if norm == "linear":
            return Normalize(vmin, vmax)
        raise ValueError(f"unknown norm {norm!r}; expected 'linear', 'log', or a Normalize")
    raise TypeError(f"expected a Normalize, 'log'/'linear', or None; got {type(norm).__name__}")