1

I'm trying to wrap my head around the quiver function to plot vector fields. Here's a test case:

import numpy as np
import matplotlib.pyplot as plt
X, Y = np.mgrid[1:1.5:0.5, 1:1.5:0.5]
print(X)
print(Y)
u = np.ones_like(X)
v = np.zeros_like(Y)
plt.quiver(X,Y, u, v)
plt.axis([0, 3, 0, 3], units='xy', scale=1.)
plt.show()

I am trying to get a vector of length 1, point from (1,0) to (2,0), but here is what I get:

enter image description here

I have tried adding the scale='xy' option, but the behaviour doesn't change. So how does this work?

theQman
  • 1,690
  • 6
  • 29
  • 52

1 Answers1

2

First funny mistake is that you put the quiver arguments to the axis call. ;-)

Next, looking at the documentation, it says

If scale_units is ‘x’ then the vector will be 0.5 x-axis units. To plot vectors in the x-y plane, with u and v having the same units as x and y, use angles='xy', scale_units='xy', scale=1.

So let's do as the documentation tells us,

import numpy as np
import matplotlib.pyplot as plt
X, Y = np.mgrid[1:1.5:0.5, 1:1.5:0.5]

u = np.ones_like(X)
v = np.zeros_like(Y)
plt.quiver(X,Y, u, v, units='xy', angles='xy', scale_units='xy', scale=1.)
plt.axis([0, 3, 0, 3])
plt.show()

and indeed we get a one unit long arrow:

enter image description here

ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712
  • Thanks, this looks better, but doesn't seem to generalize if I change the vector to `u = np.ones_like(X)`, `v = np.ones_like(Y)`, I get a vector from `(1,1)` to `(2,2)`, which is not of length `1`. How do I get that to scale to have length `1`? – theQman Feb 06 '18 at 15:46
  • ähm?! a vector of length 1 in x and length 1 in y direction is sqrt(2) long. So why should it be 1 unit long? – ImportanceOfBeingErnest Feb 06 '18 at 15:58
  • I would like it to keep that same direction, but be scaled to length `1`. – theQman Feb 06 '18 at 16:01
  • A vector can be [normalized](https://en.wikipedia.org/wiki/Unit_vector) by dividing by its norm. Do you need help doing that? – ImportanceOfBeingErnest Feb 12 '18 at 00:08