I'm plotting a sphere with a scalar field associated with it using mayavi.mlab.mesh
. Given some co-ordinates on that sphere, I would also like to plot points with the same colour as the mesh surface at that point.
For example:
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)
I would like to plot pts
with the same colour as the mesh surface underneath, but am unsure how to do that.
By working through some similar posts I found the answer in this case.
# 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