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?

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

4 Comments

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?
ä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?
I would like it to keep that same direction, but be scaled to length 1.
A vector can be normalized by dividing by its norm. Do you need help doing that?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.