0

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

enter image description here

tom10
  • 67,082
  • 10
  • 127
  • 137
Lucas L
  • 3
  • 3
  • 4
  • 1
    Isn't it your image that's rotate? Your gradient (GradX, GradY) should be largest when X and Y are largest (both in abs sense) and have the same sign, which is the case in your current image, but wouldn't be if you rotated the quiver plot. – tom10 Feb 08 '14 at 19:06

1 Answers1

3

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

enter image description here

tom10
  • 67,082
  • 10
  • 127
  • 137
  • Thanks so much! I was about to edit the question once I realized it was the image indeed which was wrong. Guess I have a lot to learn... – Lucas L Feb 08 '14 at 20:41