If you have predefined ranges and want to use specific colors for those ranges you can declare custom colormap. For example:
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.colors
x = np.linspace(-2,2,500)
y = np.linspace(-2,2,500)
XX, YY = np.meshgrid(x, y)
Z = np.sin(XX) * np.cos(YY)
cmap = colors.ListedColormap(['red', '#000000','#444444', '#666666', '#ffffff', 'blue', 'orange'])
boundaries = [-1, -0.9, -0.6, -0.3, 0, 0.3, 0.6, 1]
norm = colors.BoundaryNorm(boundaries, cmap.N, clip=True)
plt.pcolormesh(x,y,Z, cmap=cmap, norm=norm)
plt.colorbar()
plt.show()
Produces
Color i will be used for values between boundary i and i+1. Colors can be specified by names ('red'
, 'green'
), HTML codes ('#ffaa44'
, '#441188'
) or RGB tuples ((0.2, 0.9, 0.45)
).