Python Matplotlib Quiver wrong orientation

2019-08-30 05:19发布

问题:

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