gsplot.sample_cmap#

sample_cmap(name, *, count=None, values=None, norm=None, reverse=False)#

Sample a Matplotlib colormap as an (n, 4) RGBA array.

When both count and values are omitted, ten count-based samples are returned. Otherwise exactly one of the two controls is accepted. Count-based samples cover the inclusive interval from zero to one. Value-based samples use their finite data range; constant values map to the midpoint unless an explicit normalizer supplies bounds.

Parameters:
  • name (str | Colormap) – Matplotlib colormap name or native Colormap object.

  • count (int | None) – Number of evenly spaced samples, defaulting to ten when values is omitted.

  • values (Buffer | _SupportsArray[dtype[Any]] | _NestedSequence[_SupportsArray[dtype[Any]]] | bool | int | float | complex | str | bytes | _NestedSequence[bool | int | float | complex | str | bytes] | None) – Finite scalar values to normalize and sample.

  • norm (tuple[float, float] | _NormalizeProtocol | None) – Optional finite (vmin, vmax) pair or Matplotlib-compatible callable.

  • reverse (bool) – Whether to reverse the sampled colors.

Returns:

An independent floating-point (n, 4) RGBA array.

Return type:

numpy.ndarray

Raises:

DataError – If the name, count, values, normalizer, or control combination is invalid.

Examples

>>> import gsplot as gs
>>> colors = gs.sample_cmap("viridis", count=3)
>>> colors.shape
(3, 4)