NumPy rad2deg()
The numpy.rad2deg()
function converts angles from radians to degrees, performing element-wise transformation for array inputs.
Syntax
</>
Copy
numpy.rad2deg(x, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True)
Parameters
Parameter | Type | Description |
---|---|---|
x | array_like | Angle values in radians to be converted into degrees. |
out | ndarray, None, or tuple of ndarray and None, optional | Optional output array where the result is stored. If None, a new array is created. |
where | array_like, optional | Boolean mask specifying which elements to compute. Elements where where=False retain their original value. |
casting | str, optional | Defines the casting behavior when converting values. |
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 the corresponding angle values in degrees. If the input is a scalar, a scalar is returned.
Examples
1. Converting a Single Radian Value to Degrees
Here, we convert a single angle from radians to degrees.
</>
Copy
import numpy as np
# Define an angle in radians
angle_rad = np.pi # 180 degrees in radians
# Convert radians to degrees
angle_deg = np.rad2deg(angle_rad)
# Print the result
print("Angle in degrees:", angle_deg)
Output:
Angle in degrees: 180.0

2. Converting an Array of Radian Values
We convert an array of radian values to degrees.
</>
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]
# Convert to degrees
angles_deg = np.rad2deg(angles_rad)
# Print the results
print("Angles in radians:", angles_rad)
print("Angles in degrees:", angles_deg)
Output:
Angles in radians: [0. 0.52359878 0.78539816 1.04719755 1.57079633]
Angles in 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([np.pi/6, np.pi/4, np.pi/2])
# Create an output array with the same shape
output_array = np.empty_like(angles_rad)
# Convert radians to degrees and store in output_array
np.rad2deg(angles_rad, out=output_array)
# Print the results
print("Converted degrees:", output_array)
Output:
Converted degrees: [30. 45. 90.]

4. Using the where
Parameter
Using a condition to convert only selected elements.
</>
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])
# Define a mask (convert only where mask is True)
mask = np.array([False, True, True, False])
# Convert radians to degrees where mask is True
result = np.rad2deg(angles_rad, where=mask)
# Print the results
print("Converted degrees with mask:", result)
Output:
Converted degrees with mask: [ 0. 30. 45. 0.]

Only the angles where mask=True
are converted to degrees.