Python 的 matplotlib.pyplot.quiver 究竟是如何工作的? [英] How does Python's matplotlib.pyplot.quiver exactly work?

查看:79
本文介绍了Python 的 matplotlib.pyplot.quiver 究竟是如何工作的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图了解

如果不设置scale,matplotlib 会使用基于平均向量长度和向量数量的自动缩放算法.由于您只有一个长度大于零的向量,因此它变得非常大.添加更多向量会使箭头依次变小.

要使箭头的x和y扩展名相等,还需要进行一些调整:

  x = np.linspace(0,1,11)y = np.linspace(1,0,11)u = v = np.zeros((11,11))u [5,5] = 0.2plt.axis('等于')plt.quiver(x,y,u,v,scale = 1,单位='xy')

两个轴必须相等,并且单位必须设置为 xy .

I'm trying to understand how the quiver function 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:

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?

解决方案

matplotlib quiver does auto scaling. Set the scale to 1 to get your 0.2 units in x an 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.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.

To have equal x and y extensions of your arrow a few more adjustments are needed:

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.

这篇关于Python 的 matplotlib.pyplot.quiver 究竟是如何工作的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆