[英]Colormap for quiver3d() in Mayavi
I am trying to plot a vector field and I am using Mayavi2s quiver3d() function. 我试图绘制一个矢量场,我正在使用Mayavi2s quiver3d()函数。 I would like the arrows to be coloured according to their angle on the unit-sphere: 我希望箭头根据它们在单位球面上的角度着色:
I can do this this way: 我可以这样做:
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()
But this is very slow if I have hundreds of arrows. 但如果我有数百支箭,那么这很慢。 I can do it in a faster way: 我可以用更快的方式做到:
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()
But the I loose the colormap: 但我松开了色彩图:
How do I create something like the upper plot without having to create a new plot for each arrow? 如何在不必为每个箭头创建新图表的情况下创建类似上图的内容?
It is possible by manipulating the plot object after the fact. 可以通过在事实之后操纵绘图对象来实现。
To the quiver3d
call, add the arguments scalars=np.mod(phi, 2*np.pi)
(for instance) and scale_mode='none'
. 对于quiver3d
调用,添加参数scalars=np.mod(phi, 2*np.pi)
(例如)和scale_mode='none'
。 After the call, add the line 通话结束后,添加该行
obj.glyph.color_mode = 'color_by_scalar'
From memory, it might be possible to control separately the color and size of the arrows but this is a bit more elaborate. 从内存中,可以单独控制箭头的颜色和大小,但这有点复杂。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.