Jason Bellino
Jason Bellino

Reputation: 486

Using a mask with Mayavi 3d surface plot using mlab.surf()

When plotting a 3d surface using the mlab.surf() function, the presence of Nan values results in odd plotting behavior. Any ideas of what I may be doing wrong would be appreciated.

In[37]: import mayavi
In[38]: print mayavi.__version__
4.4.0

from mayavi import mlab
j = np.copy('some 2d-array') # where j is shape (680, 680) and contains Nans
mlab.surf(j)

enter image description here

If I replace NaNs with the minimum value, the plot looks fine, but has an artificial plane around the edges that I want clipped:

j[np.isnan(j)]=j[~np.isnan(j)].min()
mlab.surf(j) # where j has been set to j.min() where j==np.nan

enter image description here

If I then add a mask that corresponds to areas where j==j.min(), I get the weird output again (I've also tried this by creating a mask directly on the array prior to redefining the NaNs):

m = np.ma.masked_where(j==j.min(), j).mask
mlab.surf(j, mask=m)

enter image description here

Upvotes: 2

Views: 1086

Answers (1)

G M
G M

Reputation: 22449

I would try something like this:

    #first mask all the NaN:
    maskedarray = np.ma.masked_invalid(j)
    from mayavi import mlab
    mlab.figure(1, fgcolor=(0, 0, 0), bgcolor=(1, 1, 1))        
    surH = mlab.surf(maskedarray, warp_scale='auto', mask=maskedarray.mask)    
    mlab.show()

Upvotes: 2

Related Questions