python 标量_Python中的标量字段可视化

本文介绍如何使用Mayavi库在Python中为标量场可视化定制颜色查找表(Color Lookup Table)。作者尝试通过调整颜色映射来实现特定的颜色效果,但遇到了困难。最终采用了一个有效的方法,通过修改颜色传递函数实现了自定义色图。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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.

解决方案

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值