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

MatplotlibPythonData Visualization

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.

Example

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)
plt.show()

Output

raja
Published on 15-May-2021 12:29:09
Advertisements