gpt4 book ai didi

matplotlib - 在 Mayavi 体积可视化中使用感知均匀的色彩图

转载 作者:行者123 更新时间:2023-12-02 21:04:08 32 4
gpt4 key购买 nike

据我所知 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: 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()

...

最佳答案

如果将其设置为体积的 ColorTransferFunction,而不是将其设置为 colormap 参数,则它会按预期工作。

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

关于matplotlib - 在 Mayavi 体积可视化中使用感知均匀的色彩图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36946231/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com