如何在Python matplotlib中正确缩放3d Quiver图?

时间:2016-01-29 12:27:58

标签: python matplotlib 3d

当箭头在3d中使用时 this example 只能设置所有箭头的长度。 它不反映所提供箭头的实际长度。

在2d情况下起作用的参数比例似乎不起作用。

有没有办法缩放箭头,使它们的长度反映给定矢量场的长度?

2 个答案:

答案 0 :(得分:2)

这很奇怪。似乎u,v,w只是建立方向,而长度参数影响所有长度相同。您不能将数组放入length参数。

作为典型的解决方法,您可以单独绘制每个箭头:

from mpl_toolkits.mplot3d import Axes3D
import matplotlib
import numpy as np
import pylab as plt

print(matplotlib.__version__)

fig = plt.figure()
ax = fig.gca(projection='3d')

x, y, z = np.meshgrid(np.arange(-1, 1, 0.4),
                      np.arange(-1, 1, 0.4),
                      np.arange(-1, 1, 0.4))
x = x.reshape(np.product(x.shape))
y = y.reshape(np.product(y.shape))
z = z.reshape(np.product(z.shape))

scale = 0.02
u = np.sin(np.pi * x) * np.cos(np.pi * y) * np.cos(np.pi * z)
v = -np.cos(np.pi * x) * np.sin(np.pi * y) * np.cos(np.pi * z)
w = np.sqrt(2.0 / 3.0) * np.cos(np.pi * x) * np.cos(np.pi * y) * np.sin(np.pi * z)
lengths = np.sqrt(x**2+y**2+z**2)

for x1,y1,z1,u1,v1,w1,l in zip(x,y,z,u,v,w,lengths):
    ax.quiver(x1, y1, z1, u1, v1, w1, pivot = 'middle', length=l*0.5)

ax.scatter(x,y,z, color = 'black')
plt.show()

3d Quiver

答案 1 :(得分:1)

围绕着文档http://matplotlib.org/mpl_toolkits/mplot3d/api.html 这对你有用吗?

仍然不明显箭头长度是正确的,但至少现在看起来是3D

from mpl_toolkits.mplot3d import Axes3D
import matplotlib
import numpy as np

print(matplotlib.__version__)

fig = plt.figure()
ax = fig.gca(projection='3d')

x, y, z = np.meshgrid(np.arange(-1, 1, 0.4),
                      np.arange(-1, 1, 0.4),
                      np.arange(-1, 1, 0.4))
scale = 0.02
u = np.sin(np.pi * x) * np.cos(np.pi * y) * np.cos(np.pi * z)
v = -np.cos(np.pi * x) * np.sin(np.pi * y) * np.cos(np.pi * z)
w = np.sqrt(2.0 / 3.0) * np.cos(np.pi * x) * np.cos(np.pi * y) * np.sin(np.pi * z)

ax.quiver(x, y, z, u, v, w, pivot = 'middle', arrow_length_ratio = 0.02)
ax.scatter(x,y,z, color = 'black')
plt.show()

enter image description here

相关问题