Python 中的标量字段可视化 [英] Scalar fields visualisation in Python

查看:37
本文介绍了Python 中的标量字段可视化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在 Python 中可视化几个重叠的标量字段.我找到了 mayavi 库来做这种绘图.问题是我不明白如何为标量字段自定义颜色映射.我的想法是为每个字段设置一种颜色的阴影.我试图采用 一个例子,但它不起作用.这是我使用红色阴影可视化标量场的代码:

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.

推荐答案

我找不到使用 lut_manager 的解决方案,但是下面的解决方案遵循 这个 github 回复 对我有用.

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

这篇关于Python 中的标量字段可视化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆