colour_bar_index
- pyhelpers.ops.colour_bar_index(cmap, n_colours, labels=None, **kwargs)
Create a colour bar.
To stop making off-by-one errors. Takes a standard colour ramp, and discretizes it, then draws a colour bar with correctly aligned labels.
See also [OPS-CBI-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
labels (list or None) – a list of labels for the colour bar, defaults to
None
kwargs – [optional] parameters of matplotlib.pyplot.colorbar
- Returns
a colour bar object
- Return type
matplotlib.colorbar.Colorbar
Examples:
>>> from pyhelpers.ops import colour_bar_index >>> import matplotlib >>> import matplotlib.pyplot as plt >>> import matplotlib.cm >>> matplotlib.use('TkAgg') >>> plt.figure(figsize=(2, 6)) >>> cbar = colour_bar_index(cmap=matplotlib.cm.get_cmap('Accent'), n_colours=5) >>> plt.xticks(fontsize=12) >>> plt.yticks(fontsize=12) >>> cbar.ax.tick_params(labelsize=14) >>> # plt.axis('off') >>> plt.tight_layout() >>> plt.show()
The above example is illustrated in Fig. 5:
Fig. 5 An example of colour bar with numerical index, created by the function
colour_bar_index()
.>>> plt.figure(figsize=(2, 6)) >>> labels_ = list('abcde') >>> cbar = colour_bar_index(matplotlib.cm.get_cmap('Accent'), n_colours=5, labels=labels_) >>> plt.xticks(fontsize=12) >>> plt.yticks(fontsize=12) >>> cbar.ax.tick_params(labelsize=14) >>> # plt.axis('off') >>> plt.tight_layout() >>> plt.show()
This second example is illustrated in Fig. 6:
Fig. 6 An example of colour bar with textual index, created by the function
colour_bar_index()
.>>> plt.close(fig='all')