理解Matplotlib的箭图

2024-04-27 16:57:48 发布

您现在位置:Python中文网/ 问答频道 /正文

我想了解plt.quiver()是如何工作的。我的问题如下:

我绘制了一个简单的向量(1,1),如下所示:

import numpy as np
import matplotlib.pyplot as plt

fig = plt.figure(2)
ax = fig.add_subplot(111)
ax.quiver(0,0, 1, 1, units = 'xy', scale = 1)
plt.xticks(range(-5,6))
plt.yticks(range(-5,6))
plt.grid()

我希望箭头从(0,0)变为(1,1),但结果与此稍有不同:vector 1,1

类似地,我尝试为向量(0,3)绘制一个箭头,得到的箭头似乎是为向量(0,3.5)。。。

enter image description here

我的假设是这与kwargs'units''scale''angles'、&;'scale_units'有关。我读过关于它们的文档,但不完全理解它们是如何工作的。我们非常感谢主日学校的解释!


Tags: importnumpymatplotlibasnpfig绘制range
2条回答

如果将图形的纵横比调整为1,则矢量将显示为适当的比例:

import numpy as np
import matplotlib.pyplot as plt

fig = plt.figure()
ax = fig.add_subplot(111)
ax.quiver((0,0), (0,0), (1,0), (1,3), units = 'xy', scale = 1)
plt.axis('equal')
plt.xticks(range(-5,6))
plt.yticks(range(-5,6))
plt.grid()
plt.show()

enter image description here

你可以试试这个代码。

import matplotlib.pyplot as plt

fig = plt.figure(2)
ax = fig.add_subplot(111)
ax.quiver(0,0, 1, 1,angles='xy', scale_units='xy', scale = 1)
plt.xticks(range(-5,6))
plt.yticks(range(-5,6))
plt.grid()
plt.draw()
plt.show()

This is the output

请记住,quiver的前两个参数是向量尾部的x和y坐标,后两个参数分别是向量沿x和y方向的长度。angle='xy'使箭头从矢量的尾部指向其顶端。

您可以在http://matplotlib.org/1.3.1/api/pyplot_api.html#matplotlib.pyplot.quiver找到关于matplotlib.quiver的更多信息

相关问题 更多 >