简体   繁体   English

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

[英]Plotting 3D points with different colors in Mayavi (Python)

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? 有什么方法可以给mayavi一个元组列表,或者一些numpy数组number_of_points x 3大小,以便我可以为每个点指定不同的颜色?

So, I have the following data: 因此,我有以下数据:

x of size Nx1 (contains x coordinates of N points) 大小为Nx1的x(包含N个点的x坐标)

y of size Nx1 (contains y coordinates of N points) 大小为Nx1的y(包含N个点的y坐标)

z of size Nx1 (contains z coordinates of N points) 大小为Nx1的z(包含N个点的z坐标)

R of size Nx1 (contains the values for the R channel of N points) 大小为Nx1的R(包含N点的R通道的值)

G of size Nx1 (contains the values for the G channel of N points) 大小为Nx1的G(包含N点的G通道的值)

B of size Nx1 (contains the values for the B channel of N points) 大小为Nx1的B(包含N个点的B通道的值)

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: 我想以某种方式将此RGB数据提供给mayavi,以便它将使用该点的实际颜色,所以我想要这样的内容:

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

This works if N = 1, or with other words, only if I gave Mayavi a single point, otherwise it doesn't. 如果N = 1,或者换句话说,只有当我给Mayavi单点时,这才起作用,否则就不行。 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? 关于如何做的任何想法?

One way is to put your RGB arrays into a lookup table that you then tell your points3d object to use. 一种方法是将RGB数组放入查找表,然后告诉您要使用的points3d对象。 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()

Produces 产生

在此处输入图片说明

Reference: 参考:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM