Skip to main content

Python NumPy: How to Convert Radians to Degrees in NumPy

When working with angles in Python, you will frequently need to convert between radians and degrees. NumPy's trigonometric functions such as np.sin(), np.cos(), and np.arctan() all operate in radians, but many real-world applications, from navigation to game development, use degrees. NumPy provides dedicated vectorized functions that handle this conversion efficiently on arrays of any size and shape without requiring explicit loops.

In this guide, you will learn how to convert between radians and degrees using NumPy's built-in functions, handle floating-point precision, and apply these conversions in practical scenarios.

Converting Radians to Degrees

Use np.rad2deg() or its alias np.degrees() to convert radian values into degrees:

import numpy as np

radians = np.array([np.pi, np.pi/2, np.pi/4, np.pi/6])

degrees = np.rad2deg(radians)
# or equivalently: degrees = np.degrees(radians)

print(degrees)

Output:

[180.  90.  45.  30.]

np.rad2deg() vs. np.degrees(): Are They the Same?

Both functions produce identical results. The choice between them is purely a matter of readability preference:

import numpy as np

rad = np.pi / 3 # 60 degrees

print(np.rad2deg(rad))
print(np.degrees(rad))

# They are different function objects but produce the same output
print(np.rad2deg(rad) == np.degrees(rad))

Output:

59.99999999999999
59.99999999999999
True

Converting Degrees to Radians

The reverse conversion uses np.deg2rad() or its alias np.radians():

import numpy as np

degrees = np.array([0, 30, 45, 60, 90, 180, 360])

radians = np.deg2rad(degrees)
# or equivalently: radians = np.radians(degrees)

print(radians)

# Display as multiples of pi for readability
print(radians / np.pi)

Output:

[0.         0.52359878 0.78539816 1.04719755 1.57079633 3.14159265
6.28318531]
[0. 0.16666667 0.25 0.33333333 0.5 1.
2. ]

Working with Common Angles

Here is a quick reference showing how standard radian values map to their degree equivalents:

import numpy as np

common_radians = {
'π': np.pi,
'π/2': np.pi / 2,
'π/3': np.pi / 3,
'π/4': np.pi / 4,
'π/6': np.pi / 6,
'2π': 2 * np.pi
}

print("Radians to Degrees:")
for name, rad in common_radians.items():
deg = np.rad2deg(rad)
print(f" {name:>4} = {deg:>6.1f}°")

Output:

Radians to Degrees:
π = 180.0°
π/2 = 90.0°
π/3 = 60.0°
π/4 = 45.0°
π/6 = 30.0°
2π = 360.0°

Conversion on Multi-Dimensional Arrays

Both conversion functions are fully vectorized and work on arrays of any shape, including 2D matrices and higher-dimensional tensors:

import numpy as np

# 2D array of angles in radians
rad_matrix = np.array([
[0, np.pi/4, np.pi/2],
[np.pi, 3*np.pi/2, 2*np.pi]
])

deg_matrix = np.rad2deg(rad_matrix)
print(deg_matrix)

Output:

[[  0.  45.  90.]
[180. 270. 360.]]

No loops or reshaping are needed. NumPy applies the conversion element-wise across the entire array.

Handling Floating-Point Precision

Angle conversions can introduce tiny floating-point errors due to the irrational nature of pi. These errors are extremely small (on the order of 10 to the power of negative 14) but can be surprising if you expect exact integer results:

import numpy as np

# This should be exactly 180, but floating-point arithmetic disagrees
result = np.rad2deg(np.pi)
print(f"Raw result: {result}")
print(f"Rounded: {round(result)}")

# Multiple angles showing precision artifacts
angles = np.rad2deg(np.array([np.pi/6, np.pi/4, np.pi/3]))
print(f"Raw: {angles}")
print(f"Rounded: {np.round(angles, 10)}")

Output:

Raw result: 180.0
Rounded: 180
Raw: [30. 45. 60.]
Rounded: [30. 45. 60.]
tip

Use np.round() when displaying degree values to users or when exact integer degree values matter. These tiny precision errors rarely affect calculations, but they can cause confusion in printed output or equality comparisons.

Why Use NumPy Functions Instead of Manual Conversion

You can technically convert angles using the formula degrees = radians * 180 / pi, but the dedicated NumPy functions are preferable for several reasons.

The manual approach works but is verbose and prone to mistakes like accidentally dividing instead of multiplying:

import numpy as np

radians = np.array([np.pi/2, np.pi, 3*np.pi/2])

# Manual conversion (works but less readable)
degrees_manual = radians * 180 / np.pi

# NumPy function (clearer intent)
degrees_numpy = np.rad2deg(radians)

print(np.allclose(degrees_manual, degrees_numpy))

Output:

True

The results are identical, but np.rad2deg(radians) immediately communicates intent to anyone reading the code, whereas radians * 180 / np.pi requires a moment of interpretation.

Practical Example: Trigonometry with Degree Inputs

Since all NumPy trigonometric functions expect radians, you need to convert degree inputs before passing them in. Similarly, inverse trigonometric functions return radians, so you need to convert their output back to degrees for human-readable results:

import numpy as np

# Calculate sine of 30 degrees
angle_deg = 30
angle_rad = np.deg2rad(angle_deg)

sin_value = np.sin(angle_rad)
print(f"sin(30°) = {sin_value}")

# Calculate angle from a trig result (returns radians, convert to degrees)
cos_value = 0.5
angle_rad = np.arccos(cos_value)
angle_deg = np.rad2deg(angle_rad)
print(f"arccos(0.5) = {angle_deg}°")

Output:

sin(30°) = 0.49999999999999994
arccos(0.5) = 60.00000000000001°

Normalizing Angles to a Standard Range

After performing angle arithmetic, results can fall outside the conventional 0 to 360 degree range. These utility functions bring angles back into standard ranges:

import numpy as np

def normalize_degrees(angles):
"""Normalize angles to the [0, 360) range."""
return np.mod(angles, 360)

def normalize_to_signed(angles):
"""Normalize angles to the [-180, 180) range."""
return np.mod(angles + 180, 360) - 180

angles = np.array([-90, 450, 720, -270])
print(f"Original: {angles}")
print(f"[0, 360): {normalize_degrees(angles)}")
print(f"[-180, 180): {normalize_to_signed(angles)}")

Output:

Original:     [ -90  450  720 -270]
[0, 360): [270 90 0 90]
[-180, 180): [-90 90 0 90]

Quick Reference

ConversionPrimary FunctionAlias
Radians to Degreesnp.rad2deg(x)np.degrees(x)
Degrees to Radiansnp.deg2rad(x)np.radians(x)
ConstantValueDegrees
np.pi3.14159...180°
np.pi / 21.57079...90°
np.pi / 40.78539...45°
2 * np.pi6.28318...360°
  • Use np.rad2deg() to convert radians to degrees.
  • Use np.deg2rad() for the reverse (from degrees to radians).

Both functions are vectorized, working efficiently on scalars, 1D arrays, and multi-dimensional arrays alike. Prefer these built-in functions over manual multiplication with pi for clearer, less error-prone code.