AFAIK Mayavi没有任何感性统一的彩色地图。我天真地尝试传递它one of Matplotlib's colormaps但它失败了:
from mayavi import mlab
import multiprocessing
import matplotlib.pyplot as plt
plasma = plt.get_cmap('plasma')
...
mlab.pipeline.volume(..., colormap=plasma)
TraitError:无法设置'VolumeFactory'对象的未定义'colormap'属性。
编辑:我找到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 :(得分:3)
如果将其设置为卷的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()
答案 1 :(得分:1)
尽管like444先前的回答在某种程度上帮助了我解决类似的问题,但它会导致颜色图之间的翻译不正确。这是因为matplotlib和tvtk存储颜色信息的格式略有不同:Matplotlib使用RGBA,而ColorTransferFunction使用VRGB,其中V是显示数据中这部分颜色图所分配的值。因此,通过进行一对一复制,绿色变成红色,蓝色变成绿色,而alpha变成蓝色。以下代码段修复了该问题:
def cmap_to_ctf(cmap_name):
values = list(np.linspace(0, 1, 256))
cmap = cm.get_cmap(cmap_name)(values)
transfer_function = ctf.ColorTransferFunction()
for i, v in enumerate(values):
transfer_function.add_rgb_point(v, cmap[i, 0], cmap[i, 1], cmap[i, 2])
return transfer_function