【发布时间】:2017-06-28 15:19:22
【问题描述】:
我正在尝试绘制一个矢量场,并且我正在使用 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()
如何在不必为每个箭头创建新图的情况下创建类似于上图的内容?
【问题讨论】:
标签: python python-2.7 plot mayavi