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.


  • 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.


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)'Color Bar Title')


Updated on 06-May-2021 13:36:51