I generate some data within a loop and like to plot them as a contour plot. Each plot should use the same colormap and a specified number of contour levels. Also, the first level color should be white.
The figure generated by the following snippet resembles the problem I face at the moment:
import numpy as np
import matplotlib.pyplot as plt
x = y = np.linspace(-3, 3, 200)
data = 1800*np.exp(-(x[newaxis, :].T**2 + y**2))
plt.figure(1000)
plt.clf()
plt.contourf(x, y, data, 8, cmap=plt.cm.OrRd)
plt.axes().set_aspect("equal")
plt.colorbar()
plt.contour(x, y, data, 8, colors='k')
There are 9 and not 8 contour levels which I noticed seems to depend on the min/max values of the data. So the number of contour levels is not identical in the figures I generate within my loop since the data changes. And which colormap can I use so that the first level is white? I know there are gazillions of questions about colormaps out there. But they tend to generate a colormap by hand. I just want to use this existing one and either add white as first color or somehow modify my script so that it does start with white (which I thought it would anyway).