Search code examples
pythonenthoughtmayavicolormap

Get continuous colormap in mlab.contour3d


Why is the colormap discretized in 3 different colors when I expect it to be continuous here?

import numpy as np
from mayavi import mlab

cube = np.zeros((100,100,100))
cube[10:90,10:90,10:90] = np.linspace(0,1,80)
mlab.contour3d(cube, colormap="jet", opacity=0.5)

Mayavi output

Also, why is there only one color on my cube if don't set opacity to be below 1?


Solution

  • A contour is the curve (in 3D the surface) that visits points of equal value. For numerical data, it will lie on the points of the grid or between them by interpolation.

    The "outer" part of your cube is all zeros, so there is a jump at index 10 and 90 that creates the contour surface.

    The inner boundaries are created when the values in your grid cross the equispaced contour values (there are apparently 3 values in total by default).

    Finally, as the outer part of the cube is all zero, without transparency you will only see that and no variation in value implies on variation in color.

    I give an example below with more levels (9) and without the zero boundary, that gives horizontal contour planes.

    import numpy as np
    from mayavi import mlab
    
    cube = np.zeros((100,100,100))
    cube[:,:,:] = np.linspace(0, 1, np.prod(cube.shape)).reshape(cube.shape).T
    mlab.contour3d(cube, colormap="jet", opacity=0.5, contours=9)
    mlab.show()
    

    You can also consider "cut planes" that produce 2D slices in 3D data. There are examples in these pages: http://docs.enthought.com/mayavi/mayavi/mlab_case_studies.html and http://docs.enthought.com/mayavi/mayavi/auto/examples.html