cmap_discretisation
- pyhelpers.ops.cmap_discretisation(cmap, n_colours)[source]
Create a discrete colour ramp.
See also [OPS-CD-1].
- Parameters:
cmap (matplotlib.colors.ListedColormap | matplotlib.colors.LinearSegmentedColormap | str) – a colormap instance, e.g. built-in colormaps that is available via matplotlib.colormaps.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 >>> from pyhelpers.settings import mpl_preferences >>> mpl_preferences(backend='TkAgg', font_name='Times New Roman') >>> import matplotlib >>> import matplotlib.pyplot as plt >>> import numpy as np >>> cm_accent = cmap_discretisation(cmap=matplotlib.colormaps['Accent'], n_colours=5) >>> cm_accent.name 'Accent_5' >>> cm_accent = cmap_discretisation(cmap='Accent', n_colours=5) >>> cm_accent.name 'Accent_5' >>> fig = plt.figure(figsize=(10, 2), constrained_layout=True) >>> ax = fig.add_subplot() >>> ax.imshow(np.resize(range(100), (5, 100)), cmap=cm_accent, interpolation='nearest') >>> plt.axis('off') >>> plt.show()
The exmaple is illustrated in Fig. 3:
>>> plt.close()