Tweaking axis labels and names orientation for 3D plots in Matplotlib

To tweak axis labels and names orientation for 3D plots in Matplotlib, you can customize label positioning, spacing, and appearance. This involves setting up a 3D projection and using specific parameters to control how axis labels are displayed.

Steps to Customize 3D Axis Labels

  • Set the figure size and adjust the padding between and around the subplots
  • Create a new figure with a white background
  • Get the current axes with 3D projection
  • Set X, Y and Z axis labels with custom linespacing
  • Plot the data points using plot() method
  • Adjust the axis distance for better visibility
  • Display the figure using show() method

Example

Here's how to create a 3D plot with customized axis labels ?

import matplotlib.pyplot as plt
import numpy as np

plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True

figure = plt.figure(facecolor='w')
ax = figure.add_subplot(111, projection='3d')

# Set axis labels with custom line spacing
xLabel = ax.set_xlabel('X-axis', linespacing=3.2)
yLabel = ax.set_ylabel('Y-axis', linespacing=3.1)
zLabel = ax.set_zlabel('Z-Axis', linespacing=3.4)

# Plot sample data
ax.plot([1, 2, 3], [1, 2, 3], [1, 4, 2])

# Adjust axis distance for better label visibility
ax.dist = 10

plt.show()

Advanced Label Customization

You can further customize axis labels with rotation, color, and font properties ?

import matplotlib.pyplot as plt
import numpy as np

fig = plt.figure(figsize=(8, 6))
ax = fig.add_subplot(111, projection='3d')

# Create sample data
x = np.linspace(0, 10, 50)
y = np.sin(x)
z = np.cos(x)

ax.plot(x, y, z)

# Customize axis labels with additional parameters
ax.set_xlabel('Time (seconds)', fontsize=12, color='blue', linespacing=2.5)
ax.set_ylabel('Amplitude Y', fontsize=12, color='red', linespacing=2.0)
ax.set_zlabel('Amplitude Z', fontsize=12, color='green', linespacing=2.8)

# Adjust viewing angle and distance
ax.view_init(elev=20, azim=45)
ax.dist = 12

plt.title('3D Plot with Custom Axis Labels')
plt.show()

Key Parameters

Parameter Description Example Value
linespacing Controls spacing between label text lines 2.0 - 4.0
fontsize Sets the font size of axis labels 10, 12, 14
color Changes the color of axis labels 'red', 'blue', '#FF5733'
ax.dist Controls distance between plot and labels 8 - 15

Conclusion

Use linespacing parameter to control axis label positioning in 3D plots. Adjust ax.dist to improve label visibility and readability. Combine with font properties for professional-looking 3D visualizations.

Updated on: 2026-03-25T23:02:17+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements