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 , 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 isTrue, the resulting array is set to theufuncresult. 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 coordinatesx1 = np.array([4, 5])x2 = np.array([3, 12])# taking the arctan2 element-wisemyarray = np.arctan2(x1, x2)print(myarray)
Explanation
- Line 1: We import the
numpymodule. - Line 4–5: We create arrays,
x1andx2, 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.