How to set a default colormap in Matplotlib?


To set a default colormap in matplotlib, we can take the following steps −

  • Create random data using numpy, array dimension 4×4.

  • Create two axes and one figure using subplots() method.

  • Display the data as an image with the default colormap.

  • Set the title of the image, for the default colormap.

  • Set the default colormap using matplotlib rcParams.

  • Display the data as an image, with set default colormap.

  • Set the title of the image, for the default colormap.

  • Adjust the padding between and around the subplots.

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

Example

import numpy as np
from matplotlib import pyplot as plt
import matplotlib as mpl
plt.rcParams["figure.figsize"] = [7.00, 3.50]
plt.rcParams["figure.autolayout"] = True
data = np.random.rand(4, 4)
fig, (ax1, ax2) = plt.subplots(2)
ax1.imshow(data)
ax1.set_title("Default colormap")
mpl.rc('image', cmap='RdYlBu_r')
ax2.imshow(data)
ax2.set_title("Set default colormap")
plt.show()

Output

Updated on: 08-May-2021

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements