python - 使用 Python matplotlib 绘制 3d 矢量

标签 python vector matplotlib 3d

我正在尝试使用 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()

关于如何调整它以制作 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()

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

produced_output

关于python - 使用 Python matplotlib 绘制 3d 矢量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27023068/

相关文章:

c++ - 创建字符串数组的有效方法

dynamic - VHDL - 动态 std_logic_vector 大小

python 和 Pandas : Strange behavior when Pandas plot histogram to a specific ax

python - 如何使用 if 语句来检查某些内容是否可见?

python - 字典中一个键的多个值

python - 如何检测我是否在 Celery worker 中运行?

python - 在行、多个键上滚动减法

c++ - 从函数返回 vector 的问题

python - 为什么 matplotlib 外推/绘制缺失值?

python - 如何使用 matplotlib 创建大型子图?