Physicist
Physicist

Reputation: 3048

forming a meshgrid with phi values

I need to make an imshow() diagram with each element showing the value of the polar angle phi (starting from the x-axis). I thought of the following code:

x = np.linspace(-3*a,3*a,1000, dtype='complex')
y = np.linspace(-3*a,3*a,1000, dtype='complex')
X,Y = np.meshgrid(x,y)
rho = np.sqrt(X**2+Y**2)
phi = np.arcsin(Y/rho)

The problem with the last line is that if I just naively use arcsin, arccos or arctan, then the sign will be incorrect in some quadrants. I need phi to vary from 0 to 2*pi, or from -pi to +pi, but with the above code, half of all the values will have the sign incorrect.

So how should I do that?

Upvotes: 0

Views: 59

Answers (1)

Warren Weckesser
Warren Weckesser

Reputation: 114811

You can use numpy.arctan2:

phi = np.arctan2(Y, X)

phi will vary between -pi and pi.

Upvotes: 2

Related Questions