Python - pyplot.quiver(X, Y, U, V) 未绘制预期结果

标签 python matplotlib plot

当尝试使用 pyplot 库中的 quiver 方法绘制向量时,我收到意外且不正确的结果。 在调试时,我将所需的数组打印到控制台并收到:

X = [0, 0, 0, 0, 40, 40, 40, 40, 80, 80, 80, 80, 120, 120, 120, 120] # X-coordinate
Y = [0, 40, 80, 120, 0, 40, 80, 120, 0, 40, 80, 120, 0, 40, 80, 120] # Y-coordinate
UN = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, -1, -1, -1] # X-component
VN = [0, 0, 0, -1, 0, 0, 0, -1, 0, -1, -1, -1, 0, 0, -1, -1] # Y-component

上面的数组是使用以下代码片段绘制的:

plot = plt.figure()
plt.quiver(X, Y, UN, VN, color='Teal', headlength=7)

plt.title('Quiver Plot, Single Colour')
plt.show(plot)

输出:

Quiver Plot

正如您所看到的,我们预计向量的长度最大为 1 个单位(0 或 1),但我们在绘图上仍然收到不正确(更长)的向量。

最佳答案

您应该使用scale_units='xy'scale=1:

代码:

import matplotlib.pyplot as plt

X = [0, 0, 0, 0, 40, 40, 40, 40, 80, 80, 80, 80, 120, 120, 120, 120] # X-coordinate
Y = [0, 40, 80, 120, 0, 40, 80, 120, 0, 40, 80, 120, 0, 40, 80, 120] # Y-coordinate
UN = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, -1, -1, -1] # X-component
VN = [0, 0, 0, -1, 0, 0, 0, -1, 0, -1, -1, -1, 0, 0, -1, -1] # Y-component

plot = plt.figure()
plt.quiver(X, Y, UN, VN, color='Teal', scale_units ='x',scale=1)

plt.title('Quiver Plot, Single Colour')
plt.xlim(-10,130)
plt.ylim(-10,130)
plt.show(plot)

结果:

enter image description here

它们在那里,但实际上很小。这是左上角的放大版本:

enter image description here

关于Python - pyplot.quiver(X, Y, U, V) 未绘制预期结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40285630/

相关文章:

Python线程和原子操作

python - 按字符拆分列表中的元素

r - 通过新函数传递图形参数

python - 如何在Python中绘制直方图?

java - 使用 Javascript(或任何客户端)绘制方程式

plot - 在 Matlab 中更改拟合图的线宽

python - 如何将自定义函数应用于 dask 数据框中的组,使用多列作为函数输入

python - 如何在 Python 中找到有多少层字典

python - 在 matplotlib 图例中分隔两个字符串

python - 使用 matplotlib 和两个列表绘制图形