Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to set the Y-axis in radians in a Python plot?
To set the Y-axis in radians in a Python plot, we need to customize the axis ticks and labels to display radian values like ?/2, ?/4, etc. This is commonly needed when plotting trigonometric functions.
Steps to Set Y-axis in Radians
- Create data points using NumPy
- Plot the data using matplotlib
- Define custom tick positions in radian units
- Set custom tick labels using LaTeX formatting for fractions
- Apply the ticks and labels using
set_yticks()andset_yticklabels()
Example
Let's plot the arctangent function and set its Y-axis in radians ?
import matplotlib.pyplot as plt
import numpy as np
# Set figure size
plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True
# Create data points
x = np.arange(-10.0, 10.0, 0.1)
y = np.arctan(x)
# Create figure and plot
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(x, y)
# Define radian unit and tick positions
unit = 0.25
y_tick = np.arange(-0.5, 0.5 + unit, unit)
# Custom labels in radian format
y_label = [r"$-\frac{\pi}{2}$", r"$-\frac{\pi}{4}$", r"$0$", r"$+\frac{\pi}{4}$", r"$+\frac{\pi}{2}$"]
# Set custom ticks and labels
ax.set_yticks(y_tick * np.pi)
ax.set_yticklabels(y_label, fontsize=10)
# Add labels for clarity
ax.set_xlabel('X values')
ax.set_ylabel('Y values (radians)')
ax.set_title('Arctangent Function with Y-axis in Radians')
plt.show()
Key Components
Tick Positions
The y_tick array defines where ticks appear on the Y-axis. Multiplying by np.pi converts to radian values ?
import numpy as np
unit = 0.25
y_tick = np.arange(-0.5, 0.5 + unit, unit)
print("Tick positions:", y_tick)
print("Radian positions:", y_tick * np.pi)
Tick positions: [-0.5 -0.25 0. 0.25 0.5 ] Radian positions: [-1.57079633 -0.78539816 0. 0.78539816 1.57079633]
LaTeX Formatting
The labels use LaTeX formatting to display mathematical fractions properly ?
# Examples of LaTeX radian labels
labels = [
r"$-\frac{\pi}{2}$", # -?/2
r"$-\frac{\pi}{4}$", # -?/4
r"$0$", # 0
r"$\frac{\pi}{4}$", # ?/4
r"$\frac{\pi}{2}$" # ?/2
]
for label in labels:
print(f"Label: {label}")
Label: $-\frac{\pi}{2}$
Label: $-\frac{\pi}{4}$
Label: $0$
Label: $\frac{\pi}{4}$
Label: $\frac{\pi}{2}$
Complete Example with Sine Function
Here's another example using a sine function with radian labeling ?
import matplotlib.pyplot as plt
import numpy as np
# Create data
x = np.linspace(-2*np.pi, 2*np.pi, 100)
y = np.sin(x)
# Create plot
fig, ax = plt.subplots(figsize=(8, 4))
ax.plot(x, y)
# Set Y-axis in radian-like fractions
y_ticks = [-1, -0.5, 0, 0.5, 1]
y_labels = [r"$-1$", r"$-\frac{1}{2}$", r"$0$", r"$\frac{1}{2}$", r"$1$"]
ax.set_yticks(y_ticks)
ax.set_yticklabels(y_labels)
# Set X-axis in radians
x_ticks = [-2*np.pi, -np.pi, 0, np.pi, 2*np.pi]
x_labels = [r"$-2\pi$", r"$-\pi$", r"$0$", r"$\pi$", r"$2\pi$"]
ax.set_xticks(x_ticks)
ax.set_xticklabels(x_labels)
ax.grid(True, alpha=0.3)
ax.set_title('Sine Function with Radian Axes')
plt.show()
Conclusion
Setting Y-axis in radians requires defining custom tick positions and using LaTeX-formatted labels with set_yticks() and set_yticklabels(). This technique is essential for displaying trigonometric functions with proper mathematical notation.
