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
Selected Reading
Rotating axes label text in 3D Matplotlib
To rotate axes label text in 3D matplotlib, we can use the set_xlabel(), set_ylabel(), and set_zlabel() methods with the rotation parameter to control the orientation of axis labels.
Basic Z-axis Label Rotation
The most common case is rotating the Z-axis label, which can overlap with tick labels in 3D plots ?
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
# Set figure size
plt.rcParams["figure.figsize"] = [8, 6]
plt.rcParams["figure.autolayout"] = True
# Create 3D plot
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
# Add some sample data
x = np.random.randn(100)
y = np.random.randn(100)
z = np.random.randn(100)
ax.scatter(x, y, z)
# Rotate Z-axis label
ax.set_zlabel('Z-Axis Label', rotation=90)
ax.set_xlabel('X-Axis')
ax.set_ylabel('Y-Axis')
plt.show()
Rotating All Axis Labels
You can rotate labels for all three axes to improve readability ?
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
fig = plt.figure(figsize=(10, 8))
ax = fig.add_subplot(111, projection='3d')
# Create sample 3D surface
x = np.linspace(-5, 5, 50)
y = np.linspace(-5, 5, 50)
X, Y = np.meshgrid(x, y)
Z = np.sin(np.sqrt(X**2 + Y**2))
ax.plot_surface(X, Y, Z, alpha=0.7)
# Rotate all axis labels
ax.set_xlabel('X-Axis Label', rotation=45)
ax.set_ylabel('Y-Axis Label', rotation=-45)
ax.set_zlabel('Z-Axis Label', rotation=90)
plt.show()
Common Rotation Angles
| Axis | Common Angles | Use Case |
|---|---|---|
| X-axis | 0°, 45° | Default or diagonal |
| Y-axis | 0°, -45° | Default or diagonal |
| Z-axis | 90°, 0° | Vertical or horizontal |
Advanced Label Positioning
For better control, you can combine rotation with label padding ?
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
fig = plt.figure(figsize=(10, 8))
ax = fig.add_subplot(111, projection='3d')
# Simple 3D line plot
t = np.linspace(0, 2*np.pi, 100)
x = np.cos(t)
y = np.sin(t)
z = t
ax.plot(x, y, z)
# Set labels with rotation and padding
ax.set_xlabel('Cosine Values', rotation=0, labelpad=10)
ax.set_ylabel('Sine Values', rotation=0, labelpad=10)
ax.set_zlabel('Time Parameter', rotation=90, labelpad=15)
plt.show()
Conclusion
Use the rotation parameter in set_xlabel(), set_ylabel(), and set_zlabel() to rotate axis labels in 3D plots. Z-axis labels commonly use 90° rotation to avoid overlapping with tick labels.
Advertisements
