Python Matplotlib Quiver wrong orientation

让人想犯罪 __ 提交于 2019-12-13 03:43:27

问题


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

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()

my plot


回答1:


I suspect you need:

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

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.

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).



来源:https://stackoverflow.com/questions/21648379/python-matplotlib-quiver-wrong-orientation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!