Python 的 matplotlib.pyplot.quiver 究竟是如何工作的?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34375345/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How does Python's matplotlib.pyplot.quiver exactly work?
提问by Fernando Bastos García
I'm trying to understand how the quiverfunction in the NumPy module works. Supposedly it allows to visualize graphically the values of two arrays, for example horizontal and vertical velocities. I have the following very simple example, but I show it just to see if you can help me to find out what I'm not doing well:
我试图了解NumPy 模块中的quiver函数是如何工作的。据说它允许以图形方式可视化两个数组的值,例如水平和垂直速度。我有以下非常简单的例子,但我展示它只是为了看看你是否可以帮助我找出我做得不好的地方:
x = np.linspace(0,1,11)
y = np.linspace(1,0,11)
u = v = np.zeros((11,11))
u[5,5] = 0.2
plt.quiver(x, y, u, v)
The code produces the following figure:
代码产生下图:
As you can see, the arrow is not an arrow, but a line and it is longer than 0.2. My intention is to get an arrow of length 0.2 and I thought I could do it using quiver
. Is it possible? Or should I better use another command?
如您所见,箭头不是箭头,而是一条线,并且长度大于 0.2。我的目的是得到一个长度为 0.2 的箭头,我认为我可以使用quiver
. 是否可以?或者我应该更好地使用另一个命令?
采纳答案by Mike Müller
matplotlib quiver does auto scaling. Set the scale to 1
to get your 0.2 units in x an y:
matplotlib quiver 会自动缩放。将比例设置为1
在 x 和 y 中获得 0.2 个单位:
x = np.linspace(0,1,11)
y = np.linspace(1,0,11)
u = v = np.zeros((11,11))
u[5,5] = 0.2
plt.quiver(x, y, u, v, scale=1)
If you don't set scale
, matplotlib uses an auto scaling algorithm based on the average vector length and the number of vectors. Since you only have one vector with a length greater zero, it becomes really big. Adding more vectors makes the arrows successively smaller.
如果您没有设置scale
,matplotlib 将使用基于平均向量长度和向量数量的自动缩放算法。由于您只有一个长度大于零的向量,因此它变得非常大。添加更多向量会使箭头逐渐变小。
To have equal x and y extensions of your arrow a few more adjustments are needed:
要使箭头的 x 和 y 扩展相等,还需要进行一些调整:
x = np.linspace(0,1,11)
y = np.linspace(1,0,11)
u = v = np.zeros((11,11))
u[5,5] = 0.2
plt.axis('equal')
plt.quiver(x, y, u, v, scale=1, units='xy')
Both axes need to be equal and the units need to be set to xy
.
两个轴需要相等,单位需要设置为xy
。
回答by David Z
The quiver function visualizes a vector field, like this:
quiver 函数将向量场可视化,如下所示:
(from the examples page). In this type of plot, the vector at a point represents the magnitude of the field vector at that point. For example, if you're visualizing velocity of a fluid, the length of the arrow represents the speed of the fluid.
(来自示例页面)。在这种类型的图中,点处的矢量表示该点处的场矢量的大小。例如,如果您正在可视化流体的速度,则箭头的长度代表流体的速度。
You can think of a vector field as a function mapping input (position) to output (the vector, e.g. velocity). The output values have no relation to the input positions; in particular, they may not even be measured in the same units! So a quiver plot can only show the relativemagnitude of the field at different points - only the relative lengths of the arrows are meaningful, not their absolute lengths.
您可以将向量场视为将输入(位置)映射到输出(向量,例如速度)的函数。输出值与输入位置无关;特别是,它们甚至可能不是以相同的单位来衡量的!所以箭袋图只能显示不同点场的相对大小——只有箭头的相对长度才有意义,而不是它们的绝对长度。
In other words, you shouldn't expect a field value of 0.2 to be represented by an arrow of length 0.2 in data units.
换句话说,您不应期望字段值 0.2 以数据单位长度为 0.2 的箭头表示。
However, matplotlib provides an option you can specify to do that: the scale_units
option to quiver
. According to the documentation, you can give scale_units = 'xy'
and it will render the arrow lengths using the same units as the axes.
但是,matplotlib 提供了一个您可以指定的scale_units
选项:quiver
. 根据文档,您可以给出scale_units = 'xy'
,它将使用与轴相同的单位呈现箭头长度。
回答by Forrest Thumb
Not related to your problem, but interesting to mention: Funny thing is that by writing u[5, 5] = 0.2
you are implyingv[5, 5] = 0.2
as well (as shown in your diagonal arrow), since before you wrote u = v = np.zeros((11, 11))
. You could avoid that by writing
与您的问题无关,但值得一提的是:有趣的是,通过写作,u[5, 5] = 0.2
您也在暗示v[5, 5] = 0.2
(如对角线箭头所示),因为在您写u = v = np.zeros((11, 11))
. 你可以通过写来避免这种情况
u = np.zeros((11, 11))
v = np.zeros((11, 11))
to make u
and v
independent from each other.
制作u
并v
相互独立。