How to reverse the colormap of an image to scalar values in Matplotib?

MatplotlibPythonData Visualization

To reverse the colormap of an image, we can take the following steps −

  • Set the figure size and adjust the padding between and around the subplots.
  • Create random data points using x and y.
  • Get the blue color map using get_cmap() method.
  • Add a subplot to the current figure at index 1.
  • Plot x and y data points using scatter() method.
  • Create a colorbar for a scalar mappable instance.
  • Plot x and y data points using scatter() method, with reversed colormap.
  • Set the title of both the axes.
  • To display the figure, use show() method.

Example

import matplotlib.pyplot as plt
import numpy as np

plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True

x = np.linspace(-2, 2, 10)
y = np.linspace(-2, 2, 10)

color_map = plt.cm.get_cmap('Blues')
plt.subplot(1, 2, 1)

sc = plt.scatter(x, y, c=x, cmap=color_map)
plt.colorbar(sc)

plt.title("Colorbar")
plt.subplot(1, 2, 2)
sc = plt.scatter(x, y, c=x, cmap=color_map.reversed())
plt.colorbar(sc)

plt.title("Reversed Colorbar")

plt.show()

Output

raja
Published on 10-Jun-2021 11:53:28
Advertisements