Mayavi fixed colorbar for all scenes

Viewed 1529

I am looking for a way to keep colorbar (and colors on the scene) constant regardless the data I display (e.g. lets say I want to have colors from 0 to 10, with the current setting mayavi will adjust colors to the data, so first plot would be from 0 to 1, second 0 to 2 etc). Normalization (vmin, vmax) are not the option since they just normalize the data.

#Minimum working example
from numpy import sin, cos, mgrid, pi, sqrt
from mayavi import mlab

mlab.figure(fgcolor=(0, 0, 0), bgcolor=(1, 1, 1))
u, v = mgrid[- 0.035:pi:0.01, - 0.035:pi:0.01]

X = 2 / 3. * (cos(u) * cos(2 * v)
    + sqrt(2) * sin(u) * cos(v)) * cos(u) / (sqrt(2) -
                                             sin(2 * u) * sin(3 * v))
Y = 2 / 3. * (cos(u) * sin(2 * v) -
    sqrt(2) * sin(u) * sin(v)) * cos(u) / (sqrt(2)
    - sin(2 * u) * sin(3 * v))
Z = -sqrt(2) * cos(u) * cos(u) / (sqrt(2) - sin(2 * u) * sin(3 * v))

for i in range(1,10):
    S = sin(u)*i
    mlab.mesh(X, Y, Z, scalars=S, colormap='YlGnBu', )
    mlab.view(.0, - 5.0, 4)
    mlab.colorbar(orientation='vertical')
    mlab.show()
    mlab.close(all=True)
2 Answers

You need to fix data range of your lut

# get the current lut manager
lut_manager = mlab.colorbar(orientation='vertical')
# fix the range
lut_manager.data_range = (0, 10)

This way the scale will be always from zero to ten, usually this can be problematic if you don't have values in all of the range (colors can be very similar)

Using the recording feature, you can see you can control this by modifying the scalar look-up-table manager:

#Minimum working example
from numpy import array, sin, cos, mgrid, pi, sqrt
from mayavi import mlab

mlab.figure(fgcolor=(0, 0, 0), bgcolor=(1, 1, 1))
u, v = mgrid[- 0.035:pi:0.01, - 0.035:pi:0.01]

X = 2 / 3. * (cos(u) * cos(2 * v)
    + sqrt(2) * sin(u) * cos(v)) * cos(u) / (sqrt(2) -
                                             sin(2 * u) * sin(3 * v))
Y = 2 / 3. * (cos(u) * sin(2 * v) -
    sqrt(2) * sin(u) * sin(v)) * cos(u) / (sqrt(2)
    - sin(2 * u) * sin(3 * v))
Z = -sqrt(2) * cos(u) * cos(u) / (sqrt(2) - sin(2 * u) * sin(3 * v))

for i in range(1,10):
    S = sin(u)*i
    _m = mlab.mesh(X, Y, Z, scalars=S, colormap='YlGnBu', )
    mlab.view(.0, - 5.0, 4)
    mlab.colorbar(orientation='vertical')
    _m.module_manager.scalar_lut_manager.use_default_range = False
    _m.module_manager.scalar_lut_manager.data_range = array([-1., 10.])
    _m.module_manager.scalar_lut_manager.scalar_bar.position = array([ 0.01,  0.15])
    _m.module_manager.scalar_lut_manager.scalar_bar.position2 = array([ 0.1,  0.8])
    mlab.show()
    mlab.close(all=True)

seems that setting scalar_bar.position resets the LUT, which I kept in the code, but you may come up with different values if you record your actions.

Related