gpt4 book ai didi

python - 如何在matplotlib中使用 `quiver()`获取单位向量?

转载 作者:太空宇宙 更新时间:2023-11-03 14:09:09 26 4
gpt4 key购买 nike

我正在尝试用quiver函数来绘制向量场。这是一个测试用例:

import numpy as np
import matplotlib.pyplot as plt
X, Y = np.mgrid[1:1.5:0.5, 1:1.5:0.5]
print(X)
print(Y)
u = np.ones_like(X)
v = np.zeros_like(Y)
plt.quiver(X,Y, u, v)
plt.axis([0, 3, 0, 3], units='xy', scale=1.)
plt.show()

我试图获取长度为 1 的向量,从 (1,0) 指向 (2,0),但这是我得到的:

enter image description here

我尝试添加 scale='xy' 选项,但行为没有改变。那么这是如何工作的呢?

最佳答案

第一个有趣的错误是您将 quiver 参数放入 axis 调用中。 ;-)

接下来,查看the documentation ,它说

If scale_units is ‘x’ then the vector will be 0.5 x-axis units. To plot vectors in the x-y plane, with u and v having the same units as x and y, use angles='xy', scale_units='xy', scale=1.

所以让我们按照文档告诉我们的去做,

import numpy as np
import matplotlib.pyplot as plt
X, Y = np.mgrid[1:1.5:0.5, 1:1.5:0.5]

u = np.ones_like(X)
v = np.zeros_like(Y)
plt.quiver(X,Y, u, v, units='xy', angles='xy', scale_units='xy', scale=1.)
plt.axis([0, 3, 0, 3])
plt.show()

我们确实得到了一个单位长箭头:

enter image description here

关于python - 如何在matplotlib中使用 `quiver()`获取单位向量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48646094/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com