np.random.seed(101)
zvals = np.random.rand(100, 100) * 10
cmap = colors.ListedColormap(['white', 'red'])
bounds = [0, 5, 10]
norm = colors.BoundaryNorm(bounds, cmap.N)
img = plt.imshow(zvals, interpolation='nearest', origin='lower', cmap=cmap,
    norm=norm)
plt.colorbar(img, cmap=cmap, norm=norm, boundaries=bounds, ticks=[0, 5, 10])
plt.savefig('redwhite.png')
plt.show()
Defining a discrete colormap for imshow in matplotlib