I am trying to produce a filled contour plot in matplotlib using contourf. Data are missing in a jagged pattern near the bottom of the plot. The contour plot turns out blank not
Correct me if I'm wrong but as I understand you have this situation:
import numpy as np
import matplotlib.pyplot as plt
# generate some data with np.nan values (the missing values)
d = np.random.rand(10, 10)
d[2, 2], d[3, 5] = np.nan, np.nan
# and in your case you actually have masked values too:
d = np.ma.array(d, mask=d < .2)
# now all of the above is just for us to get some data with missing (np.nan) and
# masked values
By plotting the above with contourf,
plt.contourf(d)
plt.show()
I get:
which doesn't show (blank) the masked values (d < .2) nor the np.nan values (d[2, 2], d[3, 5])! and you want for matplotlib to only not show the masked values. So we can do this:
# the following line is replaced by your interpolation routine for
# removing np.nan values
d[np.isnan(d)] = 1
# then because we use the masked array only the masked values will still be masked
# but the np.nan values which were replaced through the interpolation algorithm
# will show up if we do the contourf plot
plt.contourf(d)
plt.show()
I don't know how fast using the masked array is in this case, but anyways this is how I'd do it. If you want a different color instead of the blank spots (whit) you need to color the patch of the axes beneath because contourf actually doesn't plot anything where there is no data, or masked data:
# make the background dark gray (call this before the contourf)
plt.gca().patch.set_color('.25')
plt.contourf(d)
plt.show()
to get: