What is numpy.arctan2() in Numpy?

Learn via video courses
Topics Covered

Trigonometry is one of the various branches of mathematics that deals with the study of the relationship between the sides and angles of a right-angled triangle. It is one of the most popular and sought-after fields in science.

In this article, we will learn about the arctan2() function in NumPy, which helps us to find the inverse of the tangent between two radians. Finding the inverse of the tangent between two radians helps us to choose a quadrant correctly.

Syntax

The syntax of arctan2() function is:

Parameters

The parameters that go in with arctan2() function are:

  • arr1: This mandatory parameter takes in y-coordinates.
  • arr2: This mandatory parameter takes in x-coordinates.
  • out: This optional parameter represents the location where the output will be stored.
  • where: This optional parameter is none by default. If it is true, a universal value will be computed at that place in the output array.

Return Type

When we use the arctan2() function, element-wise arc tangent coordinates are returned, which are in the interval of [π/2,π/2][-π/2, π/2]

Examples

  • Basic Example using direct values

    To get hands-on with arctan2(), let's take a simple example in which we will take 2 arrays containing the x and y coordinates, respectively. After that, we will use the arctan2() function to generate the inverse of its tangent function.

    Output

  • Getting Output as Degree

    Many times, we need our trigonometric values, like the inverse of the tangent, to be in degrees rather than radians. There is a very straightforward method to convert radians into degrees using a loop.

    Output

    When we calculate the inverse of the tangent using arctan2(), we get a number that is not in degrees. Using the for loop and some calculations, we converted radians into degrees.

Difference Between arctan() and arctan2()

Arctan2 is an upgraded version of the basic arctan() function in NumPy. Here are the differences between arctan() and arctan2() in NumPy.

NumPy arctan()NumPy arctan2()
This is a 2-quadrant inverse function, which returns two quadrant valuesThis is a 4-quadrant inverse function, which returns four quadrant values
The range of arctan is from -90 to 90 degrees.The range of arctan is from -180 to 180 degrees.
This function can accept a single array as input.This function only accepts two arrays as input.

Conclusion

  • In this article, we covered what arctan2() is; a function that returns the inverse of a tangent.
  • We understood the syntax, different parameters that arctan2() takes, and the value that is returned.
  • To further solidify our understanding, we went through different examples in which we used arctan2(); used arctan2() to get direct output and also as degrees.
  • To conclude, we read the major differences between arctan() and arctan2() functions in NumPy; including the number of quadrants they return and their ranges.