MayaVi points3d 具有指定的单独颜色

MayaVi points3d with specified individual colors

我想渲染一些 3d 球体并指定每个球体的大小和颜色。尺寸我没问题:

import mayavi.mlab as mlab
import numpy as np
#white background
mlab.figure(fgcolor=(0., 0., 0.), bgcolor=(1, 1, 1))
#coordinates and sizes
x1     = np.array([27.340, 26.266, 26.913, 27.886, 25.112])
y1     = np.array([24.430, 25.413, 26.639, 26.463, 24.880])
z1     = np.array([2.614, 2.842, 3.531, 4.263, 3.649])
#initial size
size1  = np.array([7, 6, 6, 8, 6])
#decrease size of the spheres
size = np.true_divide(size1, 9)
#create spheres
mlab.points3d(x1, y1, z1, size, resolution=60, scale_factor=1)
#draw
mlab.show()

给出了 OK spheres,但是无法控制颜色。

我想根据 size1 值重新着色

colors = []
for i in size1:
    if i == 6 :
        colors.append([0.5, 0.5, 0.5]) # grey
    elif i == 7:
        colors.append([0.1, 0.1, 0.9]) # blue
    elif i == 8:
        colors.append([0.9, 0.1, 0.1]) # red

我遇到过与 here 类似的问题,但我使用 points3d 并且没有可用于我的情况的解决方案。

如何为每个 3d 定义颜色point/sphere?

好的,我找到了一个选项来处理这个问题 - 重绘 mlab.points3d 多次。 并为不同的颜色创建列表:

list_grey = []
list_red = []
list_blue = []

for i in range(np.count_nonzero(size1)):
    if size1[i] == 6 :
        list_grey.append(i)
    elif size1[i] == 7:
        list_blue.append(i)
    elif size1[i] == 8:
        list_red.append(i)

mlab.points3d(x1[list_grey], y1[list_grey], z1[list_grey], size[list_grey], resolution=60, scale_factor=1, color=(0.5, 0.5, 0.5))
mlab.points3d(x1[list_blue], y1[list_blue], z1[list_blue], size[list_blue], resolution=60, scale_factor=1, color=(0.1, 0.1, 0.9))
mlab.points3d(x1[list_red], y1[list_red], z1[list_red], size[list_red], resolution=60, scale_factor=1, color=(0.9, 0.1, 0.1))