.rad2deg()
In Numpy, the .rad2deg()
function converts angles from radians to degrees.
This is useful when working with trigonometric calculations or geometrical operations where angles need to be expressed in degrees.
Syntax
numpy.rad2deg(x, out=None, where=True, casting='same_kind', order='K', dtype=None, subok=True)
x
: Input array (or scalar) of angles in radians.out
(Optional): A location to store the output. If not provided, a new array is returned.where
(Optional): Condition to specify which elements are converted. Default isTrue
, meaning all elements are converted.
Note: There are additional parameters that are rarely used and control advanced memory and computation behaviors.
Example 1
Here is an example that demonstrates converting an array of common radian values to their equivalent degree values:
import numpy as np# Example angles in radiansangle_radians = np.array([0, np.pi / 6, np.pi / 4, np.pi / 2, np.pi])# Convert to degreesangle_degrees = np.rad2deg(angle_radians)print(angle_degrees)
The output of the above example will be as follows:
[ 0. 30. 45. 90. 180.]
Example 2
.rad2deg()
can handle negative radian values as well by converting them to their equivalent negative degree values. Here’s an example:
import numpy as np# Negative radiansangle_radians = np.array([-np.pi / 2, -np.pi, -3 * np.pi / 4])# Convert to degreesangle_degrees = np.rad2deg(angle_radians)print(angle_degrees)
The output of the above example will be as follows:
[ -90. -180. -135.]
Example 3
Additionally, the where
parameter can used to selectively convert only positive radian values to degrees, while leaving negative values unchanged:
import numpy as np# Mixed radiansangles = np.array([0, np.pi / 6, -np.pi / 4, -np.pi / 2, np.pi])# Convert only positive anglespositive_degrees = np.where(angles > 0, np.rad2deg(angles), 0)print(positive_degrees)
The output of the above code will be as follows:
[ 0. 30. 0. 0. 180.]
Codebyte Example
Run the following codebyte example to understand how the .rad2deg()
function works:
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn Python:NumPy on Codecademy
- Career path
Computer Science
Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!Includes 6 CoursesWith Professional CertificationBeginner Friendly75 hours - Course
Learn Python 3
Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.With CertificateBeginner Friendly23 hours