我正在使用 MayaVi使用 points3d
类绘制 3d 点的 Python 库。 The documentation指定每个点的颜色通过第四个参数指定,s
:
In addition, you can pass a fourth array s of the same shape as x, y, and z giving an associated scalar value for each point, or a function f(x, y, z) returning the scalar value. This scalar value can be used to modulate the color and the size of the points.
这为每个点指定一个标量值,它将点映射到颜色图,例如 copper
、jet
或 hsv
。例如。来自他们的文档:
import numpy
from mayavi.mlab import *
def test_points3d():
t = numpy.linspace(0, 4*numpy.pi, 20)
cos = numpy.cos
sin = numpy.sin
x = sin(2*t)
y = cos(t)
z = cos(2*t)
s = 2+sin(t)
return points3d(x, y, z, s, colormap="copper", scale_factor=.25)
给予:
相反,我想将每个点的实际值指定为 (r, g, b) 元组。这在 MayaVi 中可能吗?我尝试用元组数组替换 s
,但抛出了错误。
最佳答案
在今天的大部分时间里为此苦苦挣扎之后,我找到了一种相对简单的方法来准确地完成问题的要求——为每个点指定一个 RGB 元组。诀窍就是定义一个颜色图,其条目数与要绘制的点数完全相同,然后将参数设置为索引列表:
# Imports
import numpy as np
from mayavi.mlab import quiver3d, draw
# Primitives
N = 200 # Number of points
ones = np.ones(N)
scalars = np.arange(N) # Key point: set an integer for each point
# Define color table (including alpha), which must be uint8 and [0,255]
colors = (np.random.random((N, 4))*255).astype(np.uint8)
colors[:,-1] = 255 # No transparency
# Define coordinates and points
x, y, z = colors[:,0], colors[:,1], colors[:,2] # Assign x, y, z values to match color
pts = quiver3d(x, y, z, ones, ones, ones, scalars=scalars, mode='sphere') # Create points
pts.glyph.color_mode = 'color_by_scalar' # Color by scalar
# Set look-up table and redraw
pts.module_manager.scalar_lut_manager.lut.table = colors
draw()
关于python - 在 MayaVi 中指定 3D 点的绝对颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18537172/