NumPy degrees()
The numpy.degrees()
function converts angles from radians to degrees. This is useful in trigonometric calculations where degrees are preferred over radians.
Syntax
</>
Copy
numpy.degrees(x, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True)
Parameters
Parameter | Type | Description |
---|---|---|
x | array_like | Input array containing angles in radians. |
out | ndarray, None, or tuple of ndarray and None, optional | Optional output array to store the result. If not provided, a new array is created. |
where | array_like, optional | Boolean mask that selects elements for conversion. Elements where where=False retain their original value. |
casting | str, optional | Defines the casting behavior when performing the conversion. |
order | str, optional | Memory layout order of the output array. |
dtype | data-type, optional | Defines the data type of the output array. |
subok | bool, optional | Determines if subclasses of ndarray are preserved in the output. |
Return Value
Returns an array with angles converted from radians to degrees. If the input is a scalar, a scalar is returned.
Examples
1. Converting a Single Radian Value to Degrees
Convert a single angle from radians to degrees.
</>
Copy
import numpy as np
# Define an angle in radians
angle_rad = np.pi # π radians (180 degrees)
# Convert radians to degrees
angle_deg = np.degrees(angle_rad)
# Print the result
print("Degrees:", angle_deg)
Output:
Degrees: 180.0

2. Converting an Array of Radians to Degrees
Convert multiple radian values to degrees using an array.
</>
Copy
import numpy as np
# Define an array of angles in radians
angles_rad = np.array([0, np.pi/6, np.pi/4, np.pi/3, np.pi/2]) # [0, 30, 45, 60, 90] degrees in radians
# Convert radians to degrees
angles_deg = np.degrees(angles_rad)
# Print the results
print("Radians:", angles_rad)
print("Degrees:", angles_deg)
Output:
Radians: [0. 0.52359878 0.78539816 1.04719755 1.57079633]
Degrees: [ 0. 30. 45. 60. 90.]

3. Using the out
Parameter
Using an output array to store results instead of creating a new array.
</>
Copy
import numpy as np
# Define an array of angles in radians
angles_rad = np.array([0, np.pi/2, np.pi, 2*np.pi]) # [0, 90, 180, 360] degrees in radians
# Create an output array with the same shape
output_array = np.empty_like(angles_rad)
# Convert radians to degrees and store result in output_array
np.degrees(angles_rad, out=output_array)
# Print the results
print("Converted degrees:", output_array)
Output:
Converted degrees: [ 0. 90. 180. 360.]

4. Using the where
Parameter
Using a condition to convert only specific elements.
</>
Copy
import numpy as np
# Define an array of angles in radians
angles_rad = np.array([0, np.pi/4, np.pi/2, np.pi])
# Define a mask (convert only where mask is True)
mask = np.array([True, False, True, False])
# Convert radians to degrees only for selected elements
angles_deg = np.degrees(angles_rad, where=mask)
# Print the results
print("Converted degrees with mask:", angles_deg)
Output:
Converted degrees with mask: [ 0. 0. 90. 0.]

The conversion is performed only where mask=True
.