cmap_discretisation

pyhelpers.ops.cmap_discretisation(cmap, n_colours)

Create a discrete colour ramp.

See also [OPS-CD-1].

Parameters
  • cmap (matplotlib.colors.ListedColormap) – a colormap instance, e.g. built-in colormaps that is accessible via matplotlib.cm.get_cmap

  • n_colours (int) – number of colours

Returns

a discrete colormap from (the continuous) cmap

Return type

matplotlib.colors.LinearSegmentedColormap

Examples:

>>> from pyhelpers.ops import cmap_discretisation
>>> import matplotlib.cm
>>> import matplotlib.pyplot as plt
>>> import numpy

>>> cm_accent = cmap_discretisation(matplotlib.cm.get_cmap('Accent'), n_colours=5)
>>> cm_accent.name
'Accent_5'

>>> fig, ax = plt.subplots(figsize=(10, 2))

>>> ax.imshow(numpy.resize(range(100), (5, 100)), cmap=cm_accent, interpolation='nearest')

>>> plt.axis('off')
>>> plt.tight_layout()
>>> plt.show()

The exmaple is illustrated in Fig. 4:

../_images/ops-cmap_discretisation-demo.svg

Fig. 4 An example of discrete colour ramp, created by the function cmap_discretisation().

>>> plt.close()