I am trying to produce a matplotlib contourf plot that has all values under a specified value in white (including zero) and that has all nan values (representing missing data) in black. I can't seem to get the nan values to be a different color then the under/zero values.A simplified example of the problem is:
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.cm as cm
# Generate some random data for a contour plot
Z = np.random.rand(10,10)
Z[0:3,0:3] = np.nan # some bad values for set_bad
Z[0:3,7:10] = 0 # some zero values for set_under
x = np.arange(10)
y = np.arange(10)
X,Y = np.meshgrid(x, y)
# Mask the bad data:
Z_masked = np.ma.array(Z,mask=np.isnan(Z))
# Get the colormap and set the under and bad colors
colMap = cm.gist_rainbow
colMap.set_bad(color='black')
colMap.set_under(color='white')
# Create the contour plot
plt.figure(figsize=(10, 9))
contourPlot = plt.contourf(X,Y,Z_masked,cmap = colMap,vmin = 0.2)
plt.colorbar(contourPlot)
plt.show()
Using this I get the figure linked below, where both the nan values (bottom left) and zero values (bottom right) are white - I'm not sure why the nan values are not black.