How to Zoom with Axes3D in Matplotlib?

Matplotlib's Axes3D allows you to create interactive 3D plots that support zooming, rotating, and panning. While basic zooming is handled automatically through mouse interaction, you can also control zoom programmatically by setting axis limits.

Basic 3D Plot with Interactive Zoom

Create a 3D scatter plot that supports interactive zooming ?

from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt

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

fig = plt.figure()
ax = Axes3D(fig)

x = [2, 4, 6, 3, 1]
y = [1, 6, 8, 1, 3]
z = [3, 4, 10, 3, 1]

ax.scatter3D(x, y, z, c=z, alpha=1, marker='d', s=150)
ax.set_xlabel('X axis')
ax.set_ylabel('Y axis')
ax.set_zlabel('Z axis')

plt.show()

The output displays a 3D scatter plot that you can zoom interactively using your mouse.

Interactive 3D plot with zoom capability

Programmatic Zoom Control

Control zoom level by setting axis limits to focus on specific data regions ?

from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt

fig = plt.figure(figsize=(10, 8))

# Create two subplots for comparison
ax1 = fig.add_subplot(121, projection='3d')
ax2 = fig.add_subplot(122, projection='3d')

x = [2, 4, 6, 3, 1, 5, 7, 8, 2, 9]
y = [1, 6, 8, 1, 3, 5, 2, 7, 4, 6]
z = [3, 4, 10, 3, 1, 8, 6, 9, 2, 7]

# Normal view
ax1.scatter3D(x, y, z, c=z, alpha=0.8, marker='o', s=100)
ax1.set_title('Normal View')
ax1.set_xlabel('X axis')
ax1.set_ylabel('Y axis')
ax1.set_zlabel('Z axis')

# Zoomed view - restrict axis limits
ax2.scatter3D(x, y, z, c=z, alpha=0.8, marker='o', s=100)
ax2.set_xlim([2, 6])  # Zoom X-axis
ax2.set_ylim([1, 6])  # Zoom Y-axis
ax2.set_zlim([1, 8])  # Zoom Z-axis
ax2.set_title('Zoomed View')
ax2.set_xlabel('X axis')
ax2.set_ylabel('Y axis')
ax2.set_zlabel('Z axis')

plt.tight_layout()
plt.show()
Two 3D plots showing normal and zoomed views side by side

Interactive Controls

In Matplotlib's interactive mode, you can use these mouse controls for zooming ?

  • Mouse Wheel − Zoom in and out
  • Left Click + Drag − Rotate the view
  • Right Click + Drag − Pan the view
  • Middle Click + Drag − Zoom (alternative method)

Zoom with View Angles

Combine zoom with custom viewing angles for better data visualization ?

from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import numpy as np

fig = plt.figure(figsize=(12, 4))

# Generate sample data
x = np.random.randn(100)
y = np.random.randn(100)
z = np.random.randn(100)

# Different zoom levels and viewing angles
views = [(30, 45), (60, 30), (45, 60)]
zoom_levels = [(4, 4, 4), (2, 2, 2), (3, 3, 3)]

for i, (elev, azim) in enumerate(views):
    ax = fig.add_subplot(1, 3, i+1, projection='3d')
    ax.scatter3D(x, y, z, c=z, alpha=0.6, s=50)
    
    # Set zoom level
    lim = zoom_levels[i]
    ax.set_xlim([-lim[0], lim[0]])
    ax.set_ylim([-lim[1], lim[1]])
    ax.set_zlim([-lim[2], lim[2]])
    
    # Set viewing angle
    ax.view_init(elev=elev, azim=azim)
    ax.set_title(f'Elev={elev}°, Azim={azim}°')

plt.tight_layout()
plt.show()
Three 3D plots with different zoom levels and viewing angles

Conclusion

Axes3D provides both interactive and programmatic zoom control through mouse interactions and axis limit settings. Use set_xlim(), set_ylim(), and set_zlim() to programmatically control zoom levels for focused data visualization.

---
Updated on: 2026-03-25T22:38:53+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements