lair.plotter.truncate_colormap#

lair.plotter.truncate_colormap(cmap, minval=0.0, maxval=1.0, n=100)[source]#

Truncate a matplotlib colormap to a range of its values.

The min and max values (from 0 to 1) bound the range, and a new colormap is built linearly from it.

Parameters:
  • cmap (str | matplotlib.colors.Colormap) – Colormap to be truncated.

  • minval (float, optional) – Minimum value to truncate the colormap. Defaults to 0.0.

  • maxval (float, optional) – Maximum value to truncate the colormap. Defaults to 1.0.

  • n (int, optional) – Number of colors in the new colormap. Defaults to 100.

Returns:

matplotlib.colors.LinearSegmentedColormap – Truncated colormap.

Return type:

LinearSegmentedColormap

See also

terrain_cmap

The terrain colormap, truncated.

NCL_cmap

A colormap from NCL’s tables.

Examples

>>> import matplotlib.pyplot as plt
>>> import numpy as np
>>> from lair.plotter import truncate_colormap
>>> gradient = np.linspace(0, 1, 256).reshape(1, -1)
>>> fig, axes = plt.subplots(2, 1, figsize=(6, 1.2))
>>> for ax, cmap in zip(
...     axes, ["viridis", truncate_colormap("viridis", 0.3, 0.9)]
... ):
...     mesh = ax.imshow(gradient, aspect="auto", cmap=cmap)
...     ax.set_axis_off()
../_images/lair-plotter-truncate_colormap-1.png