在Mayavi(Python)中绘制具有不同颜色的3D点

问题描述:

有没有办法给mayavi一个元组列表,或者一些numpy数组number_of_points x 3大小,以便我可以为每个点指定不同的颜色?

Is there any way to give mayavi a list of tuples, or maybe some numpy array of number_of_points x 3 size, such that I can specify different colour for each point?

所以,我有以下数据:

大小为Nx1的x(包含x个N点的坐标)

x of size Nx1 (contains x coordinates of N points)

y(包含y个N点的坐标)

y of size Nx1 (contains y coordinates of N points)

大小为Nx1的z(包含N个点的z坐标)

z of size Nx1 (contains z coordinates of N points)

大小为Nx1的R(包含N个点的R通道的值)

R of size Nx1 (contains the values for the R channel of N points)

大小为Nx1的G(包含N点的G通道的值)

G of size Nx1 (contains the values for the G channel of N points)

大小为Nx1的B(包含N个点的B通道的值)

B of size Nx1 (contains the values for the B channel of N points)

我想以某种方式将此RGB数据提供给mayavi,以便它将使用该点的实际颜色,所以我想要这样的东西:

I want somehow to give this RGB data to mayavi so it will use the actual colour of the point, so I would like something like this:

from mayavi import mlab
plt = mlab.points3d(x, y, z, color = (R, G, B))

如果N = 1,或者换句话说,仅当我给Mayavi单点时,这才起作用,否则就不行.因此,我可以对其进行迭代,但是由于某种原因,它非常缓慢且难以记忆.

This works if N = 1, or with other words, only if I gave Mayavi a single point, otherwise it doesn't. Thus, I can iterate it, but it is very slow and hard on memory for some reason.

我已经尝试了很多事情,但是我似乎找不到一个可以满足我需要的方法(除了循环执行之外).有关如何执行操作的任何想法?

I've tried many things, but I can't seem to find a single approach (apart from doing it in a loop) that does what I need. Any ideas on how to do it?

一种方法是将RGB数组放入查找表中,然后告诉您要使用的points3d对象.例如:

One way is to put your RGB arrays into a lookup table that you then tell your points3d object to use. For example:

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

生产

参考: