Top label for Matplotlib colorbars

MatplotlibServer Side ProgrammingProgramming

To place a top label for colorbars, we can use colorbar's axis to set the title.

Steps

  • Create random data using numpy.
  • Use imshow() method to represent data into an image, with colormap "PuBuGn" and interpolation= "nearest".
  • Create a colorbar for a scalar mappable instance, im
  • Set the title on the ax (of colorbar) using set_title() method.
  • To display the figure, use show() method.

Example

import numpy as np
from matplotlib import pyplot as plt
plt.rcParams["figure.figsize"] = [7.00, 3.50]
plt.rcParams["figure.autolayout"] = True
data = np.random.randn(4, 4)
im = plt.imshow(data, interpolation='nearest', cmap="PuBuGn")
clb = plt.colorbar(im)
clb.ax.set_title('Color Bar Title')
plt.show()

Output

raja
Published on 06-May-2021 13:34:24
Advertisements