有没有更好的方法使用Matplotlib绘制正交轴?

2024-05-19 00:05:29 发布

您现在位置:Python中文网/ 问答频道 /正文

喂!在

我试图用matplotlibquiver函数绘制三维矢量。为了帮助可视化,我还想画出以原点为中心的正交轴。在

理想情况下,我希望移动所谓的脊椎,但根据this SO post,没有简单的解决办法。在

最后,我把坐标轴画成沿x,y,z的三个向量(见下面的代码),但我忍不住认为这是一个糟糕的解决方案。。。如有任何意见,我们将不胜感激。在

代码如下:

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

f = plt.figure(1)
ax=plt.gca()
soa = np.array([[0, 0, 0, 1, 0, 0],[0, 0, 0, 0, 1, 0],[0, 0, 0, 0, 0, 1]])
X, Y, Z, U, V, W = zip(*soa)
soa2 = np.array([[0,0,0,np.sqrt(2)/2,np.sqrt(2)/2,np.sqrt(2)/2]])
I, J, K, F, G, H = zip(*soa2)

fig = plt.figure()
ax=Axes3D(fig)
ax.quiver(X, Y, Z, U, V, W, color='black')
ax.quiver(I, J, K, F, G, H)
ax.set_xlim([-1, 1])
ax.set_ylim([-1, 1])
ax.set_zlim([-1, 1])
f.show()

下面是脚本返回的图像:

enter image description here


Tags: 代码importasnpfigpltsqrtzip

热门问题