Python 使用 matplot lib 绘制 3d 矢量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27023068/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 01:20:53  来源:igfitidea点击:

plotting 3d vectors using matplot lib

pythonvectormatplotlib3d

提问by jms1980

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.

我正在尝试使用 matplotlib 在 3d 中绘制矢量。我根据之前绘制 2d 向量的示例使用了以下代码,但为 3d 向量添加了组件。

#!/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?

关于如何调整它以制作 3d 矢量图的任何想法?

回答by Tim B

You need to use Axes3D from mplot3d in mpl_toolkits, then set the subplot projection to 3d:

您需要在mpl_toolkits中使用mplot3d中的Axes3D,然后将子图投影设置为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()

Note:Older version of matplotlib often give errors for this code. Try to use at least version 1.5

注意:旧版本的 matplotlib 通常会出现此代码的错误。尝试至少使用 1.5 版

produced_output

生产输出

回答by Dave

From other answers and comments, there is clearly a difference between matplotlib versions. However, I believe Tim B's answer does not answer the question. The quivers plotted do not represent the given vectors, since their magnitudes are not properly represented. Also, the arrowheads appear to sit at the intended start points of the vectors.

从其他答案和评论来看,matplotlib 版本之间显然存在差异。但是,我相信 Tim B 的回答并没有回答这个问题。绘制的箭袋不代表给定的向量,因为它们的大小没有正确表示。此外,箭头似乎位于向量的预期起点处。

The following, adapted from the code in the previous answer, produces the desired result in python2.7with matplotlib1.5.3. To visualise a vector, setting the pivot point to pivot='tail'and scaling the quiver by the magnitude of the vector has the desired effect. The quiver arrowhead is scaled as a ratio of the quiver length. Here I divide the scaling factor by the magnitude of the vector to make all arrowheads the same size with arrow_length_ratio=0.3/vlength.

以下内容改编自上一个答案中的代码,在python2.7with 中产生所需的结果matplotlib1.5.3。要可视化矢量,将轴心点设置为矢量pivot='tail'并按矢量的大小缩放箭袋会产生所需的效果。箭袋箭头按箭袋长度的比例缩放。在这里,我将缩放因子除以向量的大小,以使所有箭头的大小与 相同arrow_length_ratio=0.3/vlength

Bad points - My code isn't very compact. I've had to provide X,Y,Z,U,V,W in an unpacked form in order to use different kwargs for each call to ax.quiver. If anyone can suggest an edit that packs kwargs I'd be extremely grateful.

缺点 - 我的代码不是很紧凑。我必须以未打包的形式提供 X、Y、Z、U、V、W,以便对 ax.quiver 的每次调用使用不同的 kwarg。如果有人可以建议包含 kwargs 的编辑,我将不胜感激。

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

vectors=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]]) 
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
for vector in vectors:
    v = np.array([vector[3],vector[4],vector[5]])
    vlength=np.linalg.norm(v)
    ax.quiver(vector[0],vector[1],vector[2],vector[3],vector[4],vector[5],
            pivot='tail',length=vlength,arrow_length_ratio=0.3/vlength)
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()

Output: Plot of vectors as quivers with matplotlib-1.5.3.

输出: 使用 matplotlib-1.5.3 作为箭袋的向量图。