I have 3 1-D ndarrays: x, y, z
and the following code:
import numpy as np
import matplotlib.pyplot as plt
import scipy.interpolate as spinterp
## de
Got it. This seems round-about, but this was the solution:
import numpy.ma as ma
Zm = ma.masked_where(np.isnan(Z),Z)
plt.pcolormesh(X,Y,Zm.T)
If the Z matrix contains nan
's, it has to be a masked array for pcolormesh
, which has to be created with ma.masked_where
, or, alternatively,
Zm = ma.array(Z,mask=np.isnan(Z))
A slight improvement on the chosen answer
import numpy.ma as ma
Zm = ma.masked_invalid(Z)
plt.pcolormesh(X, Y, Zm.T)
masked_invalid masks all NaN values, thereby saving the need to specify
mask = np.isnan(Z)
Note that the explicit masking is no longer necessary in matplotlib master as arrays are now masked automatically internally. Will be incorporated into matplotlib >2.1. See my merged pull request https://github.com/matplotlib/matplotlib/pull/5451
So now it's as simple as
plt.pcolormesh(X,Y,Z.T)