Wednesday, July 3, 2024
HomeLanguagesPythonConvert angles from radians to degrees in a NumPy array

Convert angles from radians to degrees in a NumPy array

In this article, we will discuss how to convert array elements consisting of different angles as radian to degree.

To convert an angle from radians to degrees we need to simply multiply the radians by 180°/Π?. We can also use the numpy.rad2deg() method to perform the same operation.

Now suppose we have an array containing angles in radian now our task is to convert that array into elements containing element in degree.
Example :

Input : [Π, Π/4, Π/2, 3Π/2, 2Π)
Output : [180.  45.  90. 270. 360.]

Now let us try to implement this using Python:

Example 1 : We will be using the numpy.pi attribute for Π.




# importing the module
import numpy as np
  
# creating an array containing angles in radians
rad = np.array([np.pi, np.pi / 4,
                np.pi / 2, 3 * np.pi / 2,
                2 * np.pi])
print("The angles in radian")
print(rad)
  
# converting array from radian to degree
deg = np.rad2deg(rad)
print("\nThe angles in degree")
print(deg)


Output :

The angles in radian
[3.14159265 0.78539816 1.57079633 4.71238898 6.28318531]

The angles in degree
[180.  45.  90. 270. 360.]

Example 2 : This time the conversion will be performed without using the inbuilt method.




# importing the module
import numpy as np
  
# creating an array containing angles in radians
rad = np.array([3.14159265, 0.78539816,
                1.57079633, 4.71238898,
                6.28318531])
print("The angles in radian")
print(rad)
  
# converting array from radian to degree
deg = []
for angle in rad:
    deg.append(round(angle * (180 / np.pi)))
print("\nThe angles in degree")
print(deg)


Output :

The angles in radian
[3.14159265 0.78539816 1.57079633 4.71238898 6.28318531]

The angles in degree
[180.0, 45.0, 90.0, 270.0, 360.0]

Last Updated :
19 Aug, 2020
Like Article
Save Article

<!–

–>

Similar Reads
Related Tutorials
Calisto Chipfumbu
Calisto Chipfumbuhttp://cchipfumbu@gmail.com
I have 5 years' worth of experience in the IT industry, primarily focused on Linux and Database administration. In those years, apart from learning significant technical knowledge, I also became comfortable working in a professional team and adapting to my environment, as I switched through 3 roles in that time.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments