【发布时间】:2021-02-02 13:57:59
【问题描述】:
我遵循了这个thread 来实现绘制 3D 矢量。
当使用给定的向量示例运行代码时,它看起来不错,但是当我尝试使用自己的向量时,我得到的向量没有方向:
我正在尝试了解我的数据出了什么问题。
def plot_vectors(vectors):
fig = plt.figure()
# ax = fig.gca(projection='3d')
# ax = Axes3D(fig)
ax = fig.add_subplot(111, projection='3d')
for vector in vectors:
# v1 = np.array([vector[0], vector[1], vector[2]])
v_length = np.linalg.norm(vector)
x = vector[0]
y = vector[1]
z = vector[2]
# Make the direction data for the arrows
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='tail', length=v_length, arrow_length_ratio=0.3 / v_length)
# ax.quiver(vector[0], vector[1], vector[2], vector[3], vector[4], vector[5],
# pivot='tail', length=vlength, arrow_length_ratio=0.3 / v_length)
# ax.set_xlim([-4, 4])
# ax.set_ylim([-4, 4])
# ax.set_zlim([0, 4])
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_zlabel('z')
plt.show()
例如使用这些向量运行它:
plot_vectors([[95.38065011522669, -15.867331426507993, 267.4801091009956],
[-52.379559088282384, -1.0021180591632532, 163.80875985057938]])
【问题讨论】:
标签: python matplotlib