How to create a standard colorbar for a series of plots in Python?


To create a standard colorbar for a series of plots, we can take the following steps −

  • Create random data using numpy.

  • Create a figure and a set of subplot using subplots() method, where nrows=1 and ncols=1.

  • Display data as an image.

  • Add an axes to the figure, for colorbar.

  • Create a colorbar where mappable instance is image and cax where color will be drawn.

  • To display the figure, use show() method.

Example

import numpy as np
import matplotlib.pyplot as plt
plt.rcParams["figure.figsize"] = [7.00, 3.50]
plt.rcParams["figure.autolayout"] = True
data = np.random.rand(4, 4)
fig, ax = plt.subplots(nrows=1, ncols=1)
im = ax.imshow(data)
cax = fig.add_axes([0.9, 0.1, 0.03, 0.8])
fig.colorbar(im, cax=cax)
plt.show()

Output

Updated on: 06-May-2021

381 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements