NumPy angle()

The numpy.angle() function returns the phase (angle) of a complex number in radians (default) or degrees.

Syntax

</>
Copy
numpy.angle(z, deg=False)

Parameters

ParameterTypeDescription
zarray_likeA complex number or an array of complex numbers.
degbool, optionalReturns the angle in degrees if True, otherwise in radians (default).

Return Value

Returns an ndarray or scalar containing the counterclockwise angle (phase) from the positive real axis on the complex plane. The range of values is (-π, π] in radians or (-180, 180] degrees if deg=True.


Examples

1. Computing the Angle of a Single Complex Number

Here, we compute the phase angle of a single complex number.

</>
Copy
import numpy as np

# Define a complex number
z = 1 + 1j  # Complex number (1 + i)

# Compute the angle in radians
angle_rad = np.angle(z)

# Print the result
print("Angle in radians:", angle_rad)

Output:

Angle in radians: 0.7853981633974483

The angle returned is approximately π/4 (0.785 radians), which corresponds to 45 degrees.

2. Computing Angles for an Array of Complex Numbers

We calculate the angles of multiple complex numbers provided in an array.

</>
Copy
import numpy as np

# Define an array of complex numbers
complex_numbers = np.array([1+1j, -1+1j, -1-1j, 1-1j])

# Compute angles in radians
angles = np.angle(complex_numbers)

# Print the results
print("Complex numbers:", complex_numbers)
print("Angles in radians:", angles)

Output:

Complex numbers: [ 1.+1.j -1.+1.j -1.-1.j  1.-1.j]
Angles in radians: [ 0.78539816  2.35619449 -2.35619449 -0.78539816]

The computed angles correspond to the quadrant locations of the complex numbers on the complex plane.

3. Computing Angles in Degrees

We compute the angles in degrees instead of radians by setting deg=True.

</>
Copy
import numpy as np

# Define an array of complex numbers
complex_numbers = np.array([1+1j, -1+1j, -1-1j, 1-1j])

# Compute angles in degrees
angles_deg = np.angle(complex_numbers, deg=True)

# Print the results
print("Angles in degrees:", angles_deg)

Output:

Angles in degrees: [  45.  135. -135.  -45.]

The angles are now expressed in degrees instead of radians, making them easier to interpret.

4. Computing Angles of Purely Imaginary Numbers

We find the angles of purely imaginary numbers.

</>
Copy
import numpy as np

# Define an array of purely imaginary numbers
imag_numbers = np.array([1j, -1j])

# Compute angles in radians
angles = np.angle(imag_numbers)

# Print the results
print("Angles in radians:", angles)

Output:

Angles in radians: [ 1.57079633 -1.57079633]

The angle for 1j is π/2 (90 degrees), and for -1j it is -π/2 (-90 degrees).