Python Matplotlib Quiver 方向错误 [英] Python Matplotlib Quiver wrong orientation

查看:70
本文介绍了Python Matplotlib Quiver 方向错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个关于向量微积分的 ipython 笔记本.(至少是基本的).在这段代码中,我试图绘制函数 x^2 + XY + Y^2 的伪彩色图并在其上绘制其梯度给定的矢量场U = 2X + Y和V = 2Y + X

Im creating an ipython notebook on vector calculus. (Basics, at least). On this block of code im trying to have a pseudocolor plot of the function x^2 + XY + Y^2 and plot over it the vector field given by its gradient U = 2X+Y and V = 2Y+X

但是颤动图似乎旋转了90度,因此没有指向正确的方向

However the quiver plot appears to be rotated 90 degrees, therefore not pointing in the right direction

x = arange(-2.0, 2.0,00.1)
y = arange(-2.0, 2.0,00.1)
X,Y = meshgrid(x, y)
Z = X**2 + X*Y + Y**2
GradX = (2*X+Y)
GradY = (2*Y+X)
figure(figsize=(10, 10))
im = imshow(Z, cmap=cm.RdYlBu, interpolation='none', extent=[-2,2,-2,2])
quiver(X, Y, GradX, GradY, angles='xy', scale_units='xy')
show()

我的情节

推荐答案

我怀疑你需要:

im = imshow(Z, cmap=cm.RdYlBu, interpolation='none', extent=[-2,2,-2,2], origin='lower')

即指定 origin='lower'y=-2 放在左下角.正如我在评论中提到的,我认为这是旋转的图像(实际上是翻转的——尽管在这个图中有相同的区别),而不是箭袋图.

That is, specify origin='lower' to put y=-2 in the lower left corner. As I mentioned in the comment, I think it's the image that's rotated (actually flipped -- though it's the same difference in this plot), not the quiver plot.

分析:在你的方程中,所有的 ZGradXGradY 都应该在 (+,+)地块的(-,-)角,标志为合作".正如您发布的那样,只有箭袋图具有此属性,因此旋转的是图像.另外,总是有这些问题的图像(由于从左上角索引图像的计算机惯例与在轴上绘制Y最小值的数学惯例之间的冲突,导致计算机惯例之间存在冲突.

Analysis: In your equations, all of Z, GradX, and GradY should be largest in magnitude in the (+,+) and (-,-) corners of the plots, where the signs "cooperate". As you posted, only the quiver plot has this property, so it's the image that's rotated. In addition, it's always the image that has these issues (due to the conflict between computer convention that images are indexed from the upper left vs the mathematical convention that the smallest value of Y is plotted lowest on the axis).

这篇关于Python Matplotlib Quiver 方向错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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