What is the numpy.arctan2() function in Python?

Overview

In Python, the numpy.arctan2() function is used to return the element-wise arc tangent of x1x2\frac{x1}{x2}, choosing the quadrant correctly.

Syntax

numpy.arctan2(x1, x2, /, out=None, *, where=True)

Parameter

This function takes the following parameter values:

  • x1: This represents the y-coordinates.
  • x2: This represents the x-coordinates.
  • out: This represents a location where the result is stored. This is optional.
  • where: This is the condition over which the input is being broadcast. At a given location where this condition is True, the resulting array is set to the ufunc result. Otherwise, the resulting array retains its original value. This is optioanal.
  • **kwargs: This represents other keyword arguments.

Return value

This function returns an array of angles in radians. The range of the values is [-pi, pi].

Code example

import numpy as np
# creating an array representin the coordinates
x1 = np.array([4, 5])
x2 = np.array([3, 12])
# taking the arctan2 element-wise
myarray = np.arctan2(x1, x2)
print(myarray)

Explanation

  • Line 1: We import the numpy module.
  • Line 4–5: We create arrays, x1 and x2, representing the x and y coordinates, respectively.
  • Line 8: We implement the numpy.arctan2() function on the arrays. The result is assigned to a variable, myarray.
  • Line 10: We print the variable myarray.

Free Resources