Working with numpy.radians() function (5 examples)

Updated: February 26, 2024 By: Guest Contributor Post a comment

Overview

With the importance of numerical computing in science, engineering, and beyond, Python’s numpy library stands as a cornerstone for processing arrays and performing mathematical operations. Among its numerous functions, numpy.radians() is pivotal for converting degrees into radians, an essential conversion for a wide array of trigonometric operations. This tutorial dives deep into the numpy.radians() function, demonstrating its utility across five comprehensive examples. From basics to more advanced usage, grasp the versatility and potential of this function in Python’s numeric computation realm.

Prerequisites: Basic knowledge of Python and familiarity with the NumPy library.

Example 1: Basic Conversion

The first example is a straightforward demonstration of converting a single value from degrees to radians:

import numpy as np

degree_value = 180
radian_value = np.radians(degree_value)
print(f"Degree: {degree_value}, Radian: {radian_value}")

Output:

Degree: 180, Radian: 3.141592653589793

Example 2: Converting an Array of Values

Extending the functionality to arrays, you can convert multiple degree values into radians in a single call:

import numpy as np

degree_array = np.array([0, 90, 180, 270, 360])
radian_array = np.radians(degree_array)
print(f"Degrees: {degree_array}, Radians: {radian_array}")

Output:

Degrees: [0 90 180 270 360], Radians: [0. 1.57079633 3.14159265 4.71238898 6.28318531]

Example 3: Working with Multi-Dimensional Arrays

numpy’s broadcasting capability allows numpy.radians() to easily handle multi-dimensional arrays, showcasing the function’s adaptability to complex data structures:

import numpy as np

two_dimensional_array = np.array([[0, 45, 90], [180, 270, 360]])
radian_result = np.radians(two_dimensional_array)
print(f"Degrees:\n{two_dimensional_array}\nRadians:\n{radian_result}")

Output:

Degrees:
[[ 0 45 90]
[180 270 360]]
Radians:
[[0. 0.78539816 1.57079633]
[3.14159265 4.71238898 6.28318531]]

Example 4: In-Line Conversion for Trigonometry

Now integrating with trigonometric operations, this example demonstrates how instantly converting values can simplify calculations:

import numpy as np

angle_deg = 45
sin_value = np.sin(np.radians(angle_deg))
print(f"Sin of {angle_deg} degrees is {sin_value}")

Output:

Sin of 45 degrees is 0.7071067811865475

Example 5: Application in Data Analysis

Highlighting an application scenario, this example illustrates how numpy.radians() can be instrumental in data analysis, specifically in geographic data processing:

import numpy as np
import matplotlib.pyplot as plt

# Sample latitudes in degrees
lat_deg = np.array([-90, -45, 0, 45, 90])
# Convert to radians
lat_rad = np.radians(lat_deg)

# Plotting
plt.plot(lat_deg, lat_rad, 'o-')
plt.xlabel('Latitude (Degrees)')
plt.ylabel('Latitude (Radians)')
plt.title('Latitude Conversion from Degrees to Radians')
plt.grid(True)
plt.show()

Conclusion

The numpy.radians() function is a key staple for converting degrees to radians, supporting tasks ranging from basic conversions to complex trigonometric operations and data analysis. Through the examples provided, it’s clear that whether dealing with single values, arrays, or multidimensional data, numpy.radians() seamlessly integrates into the workflow to offer precision and efficiency in computations.