使用 Python matplotlib 绘制 3d 向量 [英] plotting 3d vectors using Python matplotlib
本文介绍了使用 Python matplotlib 绘制 3d 向量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用 matplotlib 绘制 3d 向量.我根据之前绘制 2d 向量的示例使用了以下代码,但为 3d 向量添加了组件.
I am trying to plot vectors in 3d using matplotlib. I used the following code based on a previous example of plotting 2d vectors but added components for 3d vectors.
#!/usr/bin/python
import numpy as np
import matplotlib.pyplot as plt
soa =np.array( [ [0,0,1,1,-2,0], [0,0,2,1,1,0],[0,0,3,2,1,0],[0,0,4,0.5,0.7,0]])
X,Y,Z,U,V,W = zip(*soa)
plt.figure()
ax = plt.gca()
ax.quiver(X,Y,Z,U,V,W,angles='xyz',scale_units='xyz',scale=1,color='b')
ax.set_xlim([-1,10])
ax.set_ylim([-1,10])
ax.set_zlim([10,1])
plt.draw()
plt.show()
关于如何调整它以制作 3d 矢量图的任何想法?
Any ideas on how to tweak this to make a 3d vector plot?
推荐答案
需要在mpl_toolkits中使用mplot3d中的Axes3D,然后设置subplot投影为3d:
You need to use Axes3D from mplot3d in mpl_toolkits, then set the subplot projection to 3d:
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
soa = np.array([[0, 0, 1, 1, -2, 0], [0, 0, 2, 1, 1, 0],
[0, 0, 3, 2, 1, 0], [0, 0, 4, 0.5, 0.7, 0]])
X, Y, Z, U, V, W = zip(*soa)
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.quiver(X, Y, Z, U, V, W)
ax.set_xlim([-1, 0.5])
ax.set_ylim([-1, 1.5])
ax.set_zlim([-1, 8])
plt.show()
注意: 旧版本的 matplotlib 通常会出现此代码的错误.尝试至少使用 1.5 版
Note: Older version of matplotlib often give errors for this code. Try to use at least version 1.5
这篇关于使用 Python matplotlib 绘制 3d 向量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文