在 Mayavi 中绘制不同颜色的 3D 点 (Python)
Plotting 3D points with different colors in Mayavi (Python)
有没有办法给 mayavi 一个元组列表,或者一些 number_of_points x 3 大小的 numpy 数组,这样我就可以为每个点指定不同的颜色?
所以,我有以下数据:
x 大小为 Nx1(包含 N 个点的 x 坐标)
y 大小为 Nx1(包含 N 个点的 y 坐标)
z 大小为 Nx1(包含 N 个点的 z 坐标)
大小为 Nx1 的 R(包含 N 个点的 R 通道的值)
大小为 Nx1 的 G(包含 N 个点的 G 通道的值)
大小为 Nx1 的 B(包含 N 个点的 B 通道的值)
我想以某种方式将此 RGB 数据提供给 mayavi,以便它使用点的实际颜色,所以我想要这样的东西:
from mayavi import mlab
plt = mlab.points3d(x, y, z, color = (R, G, B))
如果 N = 1 或换句话说,只有当我给 Mayavi 一个点时这才有效,否则它不会。因此,我可以对其进行迭代,但由于某种原因,它非常缓慢且难以记忆。
我已经尝试了很多东西,但我似乎无法找到一种方法(除了循环执行)来满足我的需要。有什么想法吗?
一种方法是将 RGB 数组放入查找 table 中,然后告诉 points3d
对象使用。例如:
import numpy as np
import mayavi.mlab as mlab
# Fake data from:
# http://docs.enthought.com/mayavi/mayavi/auto/mlab_helper_functions.html#points3d
t = np.linspace(0, 2 * np.pi, 20)
x = np.sin(2 * t)
y = np.cos(t)
z = np.cos(2 * t)
# Create a [0..len(t)) index that we'll pass as 's'
s = np.arange(len(t))
# Create and populate lookup table (the integer index in s corresponding
# to the point will be used as the row in the lookup table
lut = np.zeros((len(s), 4))
# A simple lookup table that transitions from red (at index 0) to
# blue (at index len(data)-1)
for row in s:
f = (row/len(s))
lut[row,:] = [255*(1-f),0,255*f,255]
# Plot the points, update its lookup table
p3d = mlab.points3d(x, y, z, s, scale_mode='none')
p3d.module_manager.scalar_lut_manager.lut.number_of_colors = len(s)
p3d.module_manager.scalar_lut_manager.lut.table = lut
mlab.draw()
mlab.show()
生产
参考:
- Enthought 文档:Custom Colormap
有没有办法给 mayavi 一个元组列表,或者一些 number_of_points x 3 大小的 numpy 数组,这样我就可以为每个点指定不同的颜色?
所以,我有以下数据:
x 大小为 Nx1(包含 N 个点的 x 坐标)
y 大小为 Nx1(包含 N 个点的 y 坐标)
z 大小为 Nx1(包含 N 个点的 z 坐标)
大小为 Nx1 的 R(包含 N 个点的 R 通道的值)
大小为 Nx1 的 G(包含 N 个点的 G 通道的值)
大小为 Nx1 的 B(包含 N 个点的 B 通道的值)
我想以某种方式将此 RGB 数据提供给 mayavi,以便它使用点的实际颜色,所以我想要这样的东西:
from mayavi import mlab
plt = mlab.points3d(x, y, z, color = (R, G, B))
如果 N = 1 或换句话说,只有当我给 Mayavi 一个点时这才有效,否则它不会。因此,我可以对其进行迭代,但由于某种原因,它非常缓慢且难以记忆。
我已经尝试了很多东西,但我似乎无法找到一种方法(除了循环执行)来满足我的需要。有什么想法吗?
一种方法是将 RGB 数组放入查找 table 中,然后告诉 points3d
对象使用。例如:
import numpy as np
import mayavi.mlab as mlab
# Fake data from:
# http://docs.enthought.com/mayavi/mayavi/auto/mlab_helper_functions.html#points3d
t = np.linspace(0, 2 * np.pi, 20)
x = np.sin(2 * t)
y = np.cos(t)
z = np.cos(2 * t)
# Create a [0..len(t)) index that we'll pass as 's'
s = np.arange(len(t))
# Create and populate lookup table (the integer index in s corresponding
# to the point will be used as the row in the lookup table
lut = np.zeros((len(s), 4))
# A simple lookup table that transitions from red (at index 0) to
# blue (at index len(data)-1)
for row in s:
f = (row/len(s))
lut[row,:] = [255*(1-f),0,255*f,255]
# Plot the points, update its lookup table
p3d = mlab.points3d(x, y, z, s, scale_mode='none')
p3d.module_manager.scalar_lut_manager.lut.number_of_colors = len(s)
p3d.module_manager.scalar_lut_manager.lut.table = lut
mlab.draw()
mlab.show()
生产
参考:
- Enthought 文档:Custom Colormap