2014-11-17 31 views
3

我需要在Python中可視化幾個重疊的標量字段。我發現mayavi庫做這種情節。問題是我不明白如何爲標量字段自定義顏色映射。我的想法是每個領域都有一種顏色的色調。我試圖採用an example,但它不起作用。在這裏有我的代碼用不同深淺的紅色可視化標量場:Python中的標量字段可視化

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() 

但輸出的情節都與一個標準的藍紅查表。

+0

您有沒有找到解決方案?我有一個類似的問題:https://stackoverflow.com/questions/36946231/using-perceptually-uniform-colormaps-in-mayavi-volumetric-visualization – crypdick

+0

嗯,當我嘗試重新分配'表'到修改後的' lut',它什麼都不做。我要提交一個錯誤報告。 – crypdick

+0

據我所知,這似乎是一個錯誤。我在這裏提交了一個錯誤報告:https://github.com/enthought/mayavi/issues/371 – crypdick

回答

0

我找不到解決方案,使用lut_manager,但下面的解決方案,以下this github reply適用於我。

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()