了解Matplotlib的箭袋绘图

RSH*_*HAP 3 python vector matplotlib

我试图了解它是如何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()
Run Code Online (Sandbox Code Playgroud)

我希望箭头从那里(0,0)开始(1,1),但结果略有偏离:矢量1,1

同样地,我尝试为矢量绘制一个箭头,(0,3)结果箭头似乎是矢量(0,3.5)...

在此输入图像描述

我的假设是,这事做与kwargs 'units','scale','angles',和'scale_units'.我已经阅读了关于它们的文档,但没有完全理解它们是如何工作的.周日学校的解释将不胜感激!

Reb*_*que 6

如果将图形的纵横比调整为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()
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述


kan*_*kar 5

你可以试试这个代码。

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()
Run Code Online (Sandbox Code Playgroud)

这是输出

只需记住,前两个参数quiver是向量尾部的 x 和 y 坐标,接下来的两个参数分别是向量沿 x 和 y 方向的长度。angle='xy'使箭头从向量的尾部指向其尖端。

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