NumPy Arctan2 – A Complete Guide

NUMPY ARCTAN2

Hello Readers! Welcome to the NumPy Arctan2 tutorial. In this tutorial, we will understand one special Trigonometric Function provided by the NumPy Library i.e. arctan2. Let’s get started.

Also read: NumPy Arctan – A Complete Guide

Arctan2 – A Quick Overview

  • arctan2 is a four-quadrant inverse trigonometric function which implies that the output angle of the arctan2 function can be in any of the four quadrants.
  • It takes two numbers as an argument.
  • The function returns a value in the range [-pi, pi] based on the values passed to it.

What is NumPy Arctan2?

NumPy Arctan2 is one of the trigonometric functions provided by the NumPy Library.

It takes two arguments x1 and x2 and returns the arctan (tan inverse) of x1/x2 choosing the quadrant correctly.​

We can access the function as NumPy.arctan2().

Syntax: numpy.arctan2(x1, x2) where x1 and x2 represent the Y-Coordinate and X-Coordinate of the point, respectively.

Also read: NumPy Arccos- A Complete Guide

Sign-Conventions in Quadrants

The XY plane is divided into four equal parts known as quadrants. The point in each quadrant has different signs for X-Coordinate and Y-Coordinate.

Quadrantsx-coordinatey-coordinate
1st QuadrantPositive (+)Positive (+)
2nd QuadrantNegative (-)Positive (+)
3rd QuadrantNegative (-)Negative (-)
4th QuadrantPositive (+)Negative (-)

Using NumPy Arctan2 Function

We are done with the theory part, let’s write some code to make our understanding clear.

import numpy as np

# Output angle is in first quadrant
print("Arctan2 of (1,1) in radians is:",np.arctan2(1,1))
print("Arctan2 of (1,1) in degrees is:",np.degrees(np.arctan2(1,1)))

# Output angle is in second quadrant
print("\nArctan2 of (1,-1) in radians is:",np.arctan2(1,-1))
print("Arctan2 of (1,-1) in degrees is:",np.degrees(np.arctan2(1,-1)))

# Output angle is in third quadrant
print("\nArctan2 of (-1,-1) in radians is:",np.arctan2(-1,-1))
print("Arctan2 of (-1,-1) in degrees is:",np.degrees(np.arctan2(-1,-1)))

# Output angle is in fourth quadrant
print("\nArctan2 of (-1,1) in radians is:",np.arctan2(-1,1))
print("Arctan2 of (-1,1) in degrees is:",np.degrees(np.arctan2(-1,1)))


Output

Arctan2 of (1,1) in radians is: 0.7853981633974483
Arctan2 of (1,1) in degrees is: 45.0

Arctan2 of (1,-1) in radians is: 2.356194490192345
Arctan2 of (1,-1) in degrees is: 135.0

Arctan2 of (-1,-1) in radians is: -2.356194490192345
Arctan2 of (-1,-1) in degrees is: -135.0

Arctan2 of (-1,1) in radians is: -0.7853981633974483
Arctan2 of (-1,1) in degrees is: -45.0

Note: In all the examples the first argument of the arctan2 function is the value of the Y-Coordinate and the second argument is the value of the X-Coordinate of the point.

Let’s understand each of the above examples.

  • Line No – 4: In this example, both the arguments are positive hence the point is in the first quadrant and the arctan of x1/x2 is computed which is equal to 45 degrees.
  • Line No – 8: In this example, the first argument(Y-Coordinate) is positive but the second argument(X-Coordinate) is negative hence the point is in the second quadrant, and the arctan of x1/x2 is computed which is equal to 135 degrees (which is also the angle in the second quadrant).
  • Line No – 12: In this example, the first argument(Y-Coordinate) is negative and the second argument (X-Coordinate) is also negative hence the point is in the third quadrant, and the arctan of x1/x2 is computed which is equal to -135 degrees (which is also the angle in 3rd quadrant in an anti-clockwise sense).
  • Line No – 16: In this example, the first argument(Y-Coordinate) is negative and the second argument (X-Coordinate) is positive hence the point is in the fourth quadrant, and the arctan of x1/x2 is computed which is equal to -45 degrees (which is also the angle in 4th quadrant)

Key Point: arctan2 calculates the arctan by choosing the quadrant correctly.

Combining NumPy Array with Arctan2

Example – 1

import numpy as np

# Example 1

# Creating a NumPy Array of the y-coordinates of the points
x1 = np.array((-1 , 1.732 , 1.414 , 0.5 , 1))

# Creating a NumPy Array of the y-coordinates of the points
x2 = np.array((1 , -1 , -0.5 , 0.5 , 1))


print("Arctan2 Values in radians :\n",np.arctan2(x1 , x2))

print("Arctan2 Values in degrees :\n",np.degrees(np.arctan2(x1 , x2)))

Output

Arctan2 Values in radians :
 [-0.78539816  2.0944078   1.9106807   0.78539816  0.78539816]
Arctan2 Values in degrees :
 [-45.         120.00072778 109.47394016  45.          45.        ]

Example – 2

import numpy as np

# Example 2

# Creating a NumPy Array of the y-coordinates of the points
a = np.array((-5 , 0.5 , 1 , -1))

# Creating a NumPy Array of the y-coordinates of the points
b = np.array((4 , 1 , -1 , -1.732))

print("Arctan2 Values in radians :\n",np.arctan2(a , b))

print("Arctan2 Values in degrees :\n",np.degrees(np.arctan2(a , b)))

Output

Arctan2 Values in radians :
 [-0.89605538  0.46364761  2.35619449 -2.61798118]
Arctan2 Values in degrees :
 [ -51.34019175   26.56505118  135.         -149.99927222]

In Example 1, x1 is a NumPy Array of the values of Y-Coordinates of the points. Similarly, x2 is a NumPy Array of the values of X-Coordinates of the points and these two arrays are passed as an argument to the arctan2 function which computes the element-wise arctan of x1/x2.

In Example 2, a is a NumPy Array of the values of Y-Coordinates of the points and b is a NumPy Array of the values of X-Coordinates of the points and these two arrays are passed as an argument to the arctan2 function which computes the element-wise-arctan2 of a/b.

Task: Use NumPy arctan2 function with the linspace function of NumPy and observe the outputs.

Difference between Arctan and Arctan2

NumPy arctanNumpy arctan2
arctan is a 2-quadrant inverse function.arctan2 is a 4-quadrant inverse function.
The range is from -90 degrees to 90 degrees.The range is from -180 degrees to 180 degrees.
Takes a single input.Takes two inputs.
Takes a Single NumPy Array as an input.Takes two NumPy Arrays as input.

So, that was a quick overview of the arctan and arctan2 functions of the NumPy Library.

Summary

That was all about the NumPy Arctan2 function. The key takeaway from this tutorial is that the arctan2 function is an extension of the arctan function. We are calculating the arctan, choosing the quadrant correctly.

Go through this article twice for making your understanding of the arctan2 function crystal clear. I will be coming up with more articles on various topics of Python. Till then keep learning and explore more interesting articles here.

References

NumPy Documentation – NumPy Arctan2