我正在尝试绘制一个矢量场,我正在使用Mayavi2s quiver3d()函数。我希望箭头根据它们在单位球面上的角度着色:
我可以这样做:
def color(theta, phi):
while(phi < 0):
phi += 2.0*np.pi
h = phi / (2.0 * np.pi)
s = 1
v = 1.0 - 0.9999999*(theta / np.pi)
print("h = {}, s = {}, v ={}".format(h,s,v))
return hsv_to_rgb(h,s,v)
def plot_nice():
x = np.array([1.,2.,3.])
y = np.array([0.,0.,0.])
z = y
phi = np.linspace(0,np.pi/2.0,3)
theta = phi
u = np.sin(theta) * np.cos(phi)
v = np.sin(theta) * np.sin(phi)
w = np.cos(theta)
obj = quiver3d(x, y, z, u, v, w,
line_width=3, colormap='hsv',
scale_factor=0.8, mode='arrow',resolution=25)
for i in range(x.shape[0]):
r,g,b = color(theta[i], phi[i])
print("R: {}, G: {}, B: {}".format(r,g,b))
obj = quiver3d(x[i], y[i], z[i], u[i], v[i], w[i],
line_width=3, color=(r,g,b), colormap='hsv',
scale_factor=0.8, mode='arrow',resolution=25)
return obj
figure(bgcolor=(1,1,1))
plot_nice()
但如果我有数百支箭,那么这很慢。我可以用更快的方式做到这一点:
def plot_fast():
x = np.array([1.,2.,3.])
y = np.array([0.,0.,0.])
z = y
phi = np.linspace(0,np.pi/2.0,3)
theta = phi
u = np.sin(theta) * np.cos(phi)
v = np.sin(theta) * np.sin(phi)
w = np.cos(theta)
obj = quiver3d(x, y, z, u, v, w,
line_width=3, colormap='hsv',
scale_factor=0.8, mode='arrow',resolution=25)
plot_fast()
如何创建类似上图的内容,而无需为每个箭头创建新的图?
答案 0 :(得分:0)
可以通过在事实之后操纵绘图对象来实现。
要quiver3d
调用,请添加参数scalars=np.mod(phi, 2*np.pi)
(例如)和scale_mode='none'
。
通话结束后,添加
obj.glyph.color_mode = 'color_by_scalar'
从记忆中,可以单独控制箭头的颜色和大小,但这更精细。