How to set default colormap in Matplotlib


Default colormap of Matplotlib leaves somewhat to be desired. It's not color-blind friendly. It's not good when printed. Personally, I use "gnuplot2" and "gray" colormaps the most.

To set the default colormap for all your plots, edit ~/.config/matplotlib/matplotlibrc, and add this line:

image.cmap: gnuplot2

To set the default colormap once per script use

import matplotlib.pyplot as plt
plt.rcParams['image.cmap'] = 'gray'  # change default colormap

I use it when I have to imshow() grayscale images often. So I can just


instead of

imshow(my_grayscale_image, cmap=cm.gray)