alphapepttools.pl.MappedColormaps#

class alphapepttools.pl.MappedColormaps(cmap, percentile=None)#

Percentile-based colormap normalization for outlier-robust visualization

Mapping a continuous colorscale to data can suffer from compression due to outliers. For example, if 90 % of values lie between 0 and 1 but 10 % of values are between 1 and 100, these extreme values will cause all other values to be compressed into a small range of colors. By applying normalization to a certain percentile range of the data, the colormap can be adjusted accordingly. Values outside the percentile will receive the same color as the minimum or maximum, respectively.

Parameters:
  • cmap (str) – Name of the colormap to be used

  • percentile (tuple[float, float], optional) – Percentile range to be used for normalization. If None, the full range of data is used. For example, (5, 95) will map colors between the 5th and 95th percentile.

Attributes table#

scalar_mappable

Return a ScalarMappable for use in colorbars

Methods table#

fit_transform(data, *[, as_hex])

Normalize data and transform it to colors

Attributes#

MappedColormaps.scalar_mappable#

Return a ScalarMappable for use in colorbars

This property provides a ScalarMappable instance that can be used to create colorbars consistent with the normalization applied in fit_transform. It uses the same vmin, vmax, and colormap, ensuring that the colorbar accurately reflects the mapping of data values to colors.

Returns:

mpl.cm.ScalarMappable ScalarMappable instance with the same colormap and normalization as used in fit_transform.

Methods#

MappedColormaps.fit_transform(data, *, as_hex=False)#

Normalize data and transform it to colors

Fits the colormap normalization to the data (determining vmin/vmax from the data or percentile range) and transforms the data values to colors. Values outside the normalization range are clipped.

Parameters:
  • data (np.ndarray) – Data to be transformed into colors. Based on this data, the colormap will be normalized. Can be any shape; colors are returned in the same shape.

  • as_hex (bool, default=False) – If True, return hex color strings. If False, return RGBA tuples.

Return type:

ndarray

Returns:

np.ndarray Array of colors with the same shape as input data. If as_hex=False, the last dimension will be 4 (RGBA). If as_hex=True, returns array of hex strings.

Examples

Transform 1D data to colors:

import numpy as np
from alphapepttools.pl.colors import MappedColormaps

data = np.array([1, 2, 3, 100])  # 100 is outlier
mapper = MappedColormaps(cmap="sequential", percentile=(5, 95))
colors = mapper.fit_transform(data)
# Returns (4, 4) array of RGBA colors

Transform 2D heatmap data:

data = np.random.randn(10, 10)
mapper = MappedColormaps(cmap="diverging")
colors = mapper.fit_transform(data)
# Returns (10, 10, 4) array of RGBA colors

Get hex colors for plotting:

data = np.array([1, 2, 3, 4, 5])
mapper = MappedColormaps(cmap="sequential")
hex_colors = mapper.fit_transform(data, as_hex=True)
# Returns array of hex strings like ['#1a2b3c', ...]