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 complete examples see How to create matplotlib colormap that treats one value specially?