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()
Any ideas on how to tweak this to make a 3d vector plot?
You need to use Axes3D from mplot3d in mpl_toolkits, then set the subplot projection to 3d:
Note: Older version of matplotlib often give errors for this code. Try to use at least version 1.5