numpy.arctan2() in Python

The element-wise arc tangent of arr1/arr2 is computed using the numpy.arctan2() function, which appropriately selects the quadrant. Selecting the quadrant ensures that the signed angle in radians between the rays commencing at the origin and going through the points (1, 0) and (x2, x1) is equal to arctan2(x1, x2).

Syntax:

Parameters :

  1. arr1 : [array_like] real-valued; y-coordinates
  2. arr2 : [array_like] real valued; x-coordinates. It must match the shape of the y-coordinates.
  3. out : [ndarray, array_like [OPTIONAL]] array of same shape as x.
  4. Where : [array similar, if desired] False value indicates to leave the value in the output alone, and True value indicates to compute the universal functions (ufunc) at that point.

Note:

Return:

Arc tangent of arr1/arr2 elementwise. The closed interval contains the values [-pi / 2, pi / 2].

Code #1: Working

Program Explanation:

The arctangent of the ratios of corresponding items in two input arrays representing x and y coordinates is computed by this Python program using the NumPy module. Next, the values of arctan2 are transformed to degrees. The resultant arctan2 values are shown together with the initial x and y coordinates by the program. Here, the arctan2 function is used to illustrate how to use coordinate data to generate angles in a two-dimensional space, which is a very helpful use.

Output:

x-coordinates :  [-1, 1, 1, -1]
y-coordinates :  [-1, -1, 1, 1]
arctan2 values : 
 [-135.  -45.   45.  135.]

Code #2: Working

Program Explanation:

This Python program demonstrates how to use the NumPy library's arctan2() function. For some input arrays, such as zero, infinity, and negative zero, it computes the arctangent values. The outcomes for two distinct scenarios (a and b) are then displayed, showing how the arctan2() function responds to a range of input conditions and generates output that matches.

Output:

a: [0.         3.14159265 0.78539816]
b: [ 1.57079633 -1.57079633]