我正在使用mayavi.mlab.mesh
绘制一个带有标量场的球体。给定该球体上的一些坐标,我还想用与该点的网格表面相同的颜色绘制点。
例如:
import numpy as np
from mayavi import mlab
# Here I construct the spherical mesh grid
phi = np.linspace(0, np.pi, 100)
theta = np.linspace(0, 2*np.pi, 100)
phi, theta = np.meshgrid(phi, theta)
x = (np.sin(phi) * np.cos(theta)).astype(np.float32)
y = (np.sin(phi) * np.sin(theta)).astype(np.float32)
z = (np.cos(phi)).astype(np.float32)
# Let's use a random scalar field to demonstrate
s = np.random.randn(*x.shape)
# Now we plot the sphere surface
plot = mlab.mesh(x, y, z, scalars=s, colormap='jet')
# Let's create some random points on the sphere that we want to additionally
# plot as mlab.points3d
pts = np.random.randn(10, 3)
pts = pts / np.linalg.norm(pts)
我想用与下面的网格表面相同的颜色绘制pts
,但不确定如何做到这一点。
通过阅读一些类似的帖子,我找到了这个案例的答案。
# First scale the scalar field to be between [0, 1]
s_scaled = ((s - np.min(s)) / (np.max(s) - np.min(s)))
# Plot the points, ensuring the colormap is the same as used with the mesh
nodes = mlab.points3d(x, y, z, scale_factor=0.05, colormap='jet')
# Set scale mode to scale by vector so that the points do not change size
nodes.glyph.scale_mode = 'scale_by_vector'
# Finally set the scalar values of the points to your scaled s vector
nodes.mlab_source.dataset.point_data.scalars = s_scaled