How to plot data into imshow() with custom colormap in Matplotlib?

To plot data into imshow() with custom colormap in matplotlib, we can take the following steps−

  • Set the figure size and adjust the padding between and around the subplots.
  • Create random data points using numpy.
  • Generate a colormap object from a list of colors.
  • Display the data as an image, i.e., on a 2D regular raster
  • To display the figure, use show() method.


from matplotlib import pyplot as plt
from matplotlib.colors import ListedColormap
import numpy as np
plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True
data = np.random.rand(5, 5)
cmap = ListedColormap(['r', 'g', 'b'])
plt.imshow(data, cmap=cmap)


Updated on: 15-May-2021

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started