2016-04-29 148 views
0

AFAIK Mayavi不具有任何感知上均勻的顏色映射。我試過天真地只是通過它one of Matplotlib's colormaps但它失敗:在Mayavi體積可視化中使用感知均勻的顏色圖

from mayavi import mlab 
import multiprocessing 
import matplotlib.pyplot as plt 

plasma = plt.get_cmap('plasma') 

... 
mlab.pipeline.volume(..., colormap=plasma) 

TraitError: Cannot set the undefined 'colormap' attribute of a 'VolumeFactory' object.


編輯:我發現a guide轉換Matplotlib色彩映射到Mayavi的顏色表。但是,由於我試圖使用感知上統一的色彩映射來使用卷,所以它不起作用。

from matplotlib.cm import get_cmap 
import numpy as np 
from mayavi import mlab 

values = np.linspace(0., 1., 256) 
lut_dict = {} 
lut_dict['plasma'] = get_cmap('plasma')(values.copy()) 

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

mlab.pipeline.volume(mlab.pipeline.scalar_field(s), vmin=0, vmax=0.8, colormap=lut_dict['plasma']) # still getting the same error 
mlab.axes() 
mlab.show() 

...

+0

發現了類似的問題在這裏:https://stackoverflow.com/questions/26970659/scalar-fields-visualisation-in-python – crypdick

+0

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

回答

0

相反,它設置爲colormap說法,如果你將其設置爲體積ColorTransferFunction,它按預期工作。

import numpy as np 
from mayavi import mlab 
from tvtk.util import ctf 
from matplotlib.pyplot import cm 

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

volume = mlab.pipeline.volume(mlab.pipeline.scalar_field(s), vmin=0, vmax=0.8) 
# save the existing colormap 
c = ctf.save_ctfs(volume._volume_property) 
# change it with the colors of the new colormap 
# in this case 'plasma' 
c['rgb']=cm.get_cmap('plasma')(values.copy()) 
# load the color transfer function to the volume 
ctf.load_ctfs(c, volume._volume_property) 
# signal for update 
volume.update_ctf = True 

mlab.show()