How to redefine a color for a specific value in a colormap

You can also use set_under which I think makes more semantic sense than using set_bad my_cmap = matplotlib.cm.get_cmap(‘rainbow’) my_cmap.set_under(‘w’) imshow(np.arange(25).reshape(5, 5), interpolation=’none’, cmap=my_cmap, vmin=.001) You can tweak the colorbar to also show the ‘under’ (and the symmetric ‘over’) color using the kwarg extend, see example and docs. For an answer to a duplicate with more … Read more

How to set default colormap in Matplotlib

To change the default colormap only for the current interactive session or one script use import matplotlib as mpl mpl.rc(‘image’, cmap=’gray’) For versions of matplotlib prior to 2.0 you have to use the rcParams dict. This still works in newer versions. import matplotlib.pyplot as plt plt.rcParams[‘image.cmap’] = ‘gray’ To change the default colormap permanently edit … Read more

How to convert a NumPy array to PIL image applying matplotlib colormap

Quite a busy one-liner, but here it is: First ensure your NumPy array, myarray, is normalised with the max value at 1.0. Apply the colormap directly to myarray. Rescale to the 0-255 range. Convert to integers, using np.uint8(). Use Image.fromarray(). And you’re done: from PIL import Image from matplotlib import cm im = Image.fromarray(np.uint8(cm.gist_earth(myarray)*255)) with … Read more