Convert angles from radians to degrees with Python rad2deg()


To convert a radian array to degrees, use the numpy.rad2deg() method in Python Numpy. The method returns the corresponding angle in degrees. This is a scalar if x is a scalar. The 1st parameter is an input angle in radians. The 2nd and 3rd parameters are optional.

The 2nd parameter is an ndarray, A location into which the result is stored. If provided, it must have a shape that the inputs broadcast to. If not provided or None, a freshly-allocated array is returned.

The 3rd parameter is the condition is broadcast over the input. At locations where the condition is True, the out array will be set to the ufunc result. Elsewhere, the out array will retain its original value.

Steps

At first, import the required library −

import numpy as np

Create an Array −

arr = np.arange(12.)

Displaying our array −

print("Array...\n",arr)

Get the datatype −

print("\nArray datatype...\n",arr.dtype)

Get the dimensions of the Array −

print("\nArray Dimensions...\n",arr.ndim)

Get the number of elements of the Array −

print("\nNumber of elements in the Array...\n",arr.size)

Radian array −

res = arr*np.pi/6

To convert a radian array to degrees, use the numpy.rad2deg() method in Python Numpy. The method returns the corresponding angle in degrees. This is a scalar if x is a scalar −

print("\nRadian array to degrees...\n",np.rad2deg(res))

Example

import numpy as np

# Create an Array
arr = np.arange(12.)

# Display the array
print("Array...\n", arr)

# Get the type of the array
print("\nOur Array type...\n", arr.dtype)

# Get the dimensions of the Array
print("\nOur Array Dimensions...\n",arr.ndim)

# Get the number of elements in the Array
print("\nNumber of elements...\n", arr.size)

# Radian array
res = arr*np.pi/6

# To convert a radian array to degrees, use the numpy.rad2deg() method in Python Numpy
# The method returns the corresponding angle in degrees. This is a scalar if x is a scalar.
print("\nRadian array to degrees...\n",np.rad2deg(res))

Output

Array...
[ 0. 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11.]

Our Array type...
float64

Our Array Dimensions...
1

Number of elements...
12

Radian array to degrees...
[ 0. 30. 60. 90. 120. 150. 180. 210. 240. 270. 300. 330.]

Updated on: 25-Feb-2022

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements