5

I need to visualize several overlapping scalar fields in Python. I found mayavi library to do this kind of plots. The problem is that I don't understand how to customize a color map for scalar fields. My idea is to have shades of one color for each field. I tried to adopt an example, but it doesn't work. Here there is my code to visualize a scalar field using shades of red:

import numpy as np
from mayavi import mlab

x, y, z = np.ogrid[-10:10:20j, -10:10:20j, -10:10:20j]
s = np.sin(x*y*z)/(x*y*z)

src = mlab.pipeline.scalar_field(s)
volume = mlab.pipeline.volume(src)

lut = np.zeros((256, 4), np.uint8)
lut[:,-1] = 255
lut[:, 0] = np.linspace(0, 255, 256)

volume.module_manager.scalar_lut_manager.lut.table = lut

mlab.draw()
mlab.view(40, 85)

mlab.show()

However, the output plot is always with a standard blue-red look-up table.

Shog9
  • 156,901
  • 35
  • 231
  • 235
desa
  • 1,240
  • 12
  • 31
  • Did you ever find a solution? I'm having a similar problem: https://stackoverflow.com/questions/36946231/using-perceptually-uniform-colormaps-in-mayavi-volumetric-visualization – crypdick Apr 29 '16 at 21:15
  • Hmm, when I try to reassign `table` to the modified `lut`, it doesn't do anything. I'm going to file a bug report. – crypdick Apr 29 '16 at 21:37
  • As far as I can tell, this seems to be a bug. I've filed a bug report here: https://github.com/enthought/mayavi/issues/371 – crypdick Apr 29 '16 at 21:50
  • No, I didn't manage to solve the problem.... Let's see how your bug report will be commented. – desa Apr 30 '16 at 17:47

1 Answers1

1

I couldn't find a solution using the lut_manager, however the solution below, following this github reply works for me.

import numpy as np
from mayavi import mlab
# import color transfer function from vtk
from tvtk.util import ctf
# import matlab colormaps
from matplotlib.pyplot import cm

x, y, z = np.ogrid[-10:10:20j, -10:10:20j, -10:10:20j]
s = np.sin(x*y*z)/(x*y*z)

src = mlab.pipeline.scalar_field(s)
volume = mlab.pipeline.volume(src)

# save the color transfer function of the current volume
c = ctf.save_ctfs(volume._volume_property)
# change the alpha channel as needed
c['alpha'][1][1] = 0.5
# change the color points to another color scheme
# in this case 'magma'
c['rgb']=[[a[0],a[1],a[2],cm.magma.colors.index(a)/255] for a in cm.magma.colors]
# load the new color transfer function
ctf.load_ctfs(c, volume._volume_property)
# signal for update
volume.update_ctf = True

mlab.show()
ilke444
  • 2,641
  • 1
  • 17
  • 31