Search code examples
pythonmatplotlibmayavicolormapmayavi.mlab

Set Nan Colour In Mayavi


I have lots of data, interpolated with separate functions, which contains gaps (NaN) values. I would like to plot these planes of data as images in MayaVi using imshow(), which I have done with some success.

To avoid the complexity of the data, consider displaying the array:

import numpy as np
from mayavi.mlab import *
grid_z0 = np.array([[1,2,3],[4,np.nan,6],[7,8,9]])
imshow(grid_z0, interpolate = False)

Snapshot without transparency:

Snapshot without transparency

In reality, I would like the gaps in my data to be transparent. (i.e. the dark red 128,0,0 square in the middle would be see through).

I'm aware that editing colormaps isn't really a thing in MayaVi (as it is in matplotlib), but I can see 'NaN color' options in the MayaVi pipeline, and documentation like this show that editing the color options is a possibility.

However, I'm stuck to see why the NaN values come out as (128,0,0) RGB, and what I can do to make them transparent.


Solution

  • More thinking, reading and fiddling:

    img = imshow(grid_z0)
    img.module_manager.scalar_lut_manager.lut.nan_color = 0, 0, 0, 0
    img.update_pipeline()