我想渲染一些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()
我想根据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点/球体定义颜色?
答案 0 :(得分:0)
好的,我找到了一个如何处理这个问题的选项 - 多次重绘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))