Make 3D plot interactive in Jupyter Notebook (Python & Matplotlib)

Interactive 3D plots in Jupyter Notebook allow you to rotate, zoom, and pan your visualizations. Matplotlib provides built-in interactivity when using the %matplotlib notebook or %matplotlib widget magic commands.

Setting Up Interactive Mode

To enable interactivity, use the appropriate magic command at the beginning of your notebook ?

%matplotlib notebook
# or use %matplotlib widget for newer versions
import matplotlib.pyplot as plt
import numpy as np

Creating an Interactive 3D Sphere

Here's how to create an interactive 3D wireframe sphere ?

import matplotlib.pyplot as plt
import numpy as np

# Create figure and 3D subplot
fig = plt.figure(figsize=(8, 6))
ax = fig.add_subplot(111, projection='3d')

# Generate sphere coordinates
u, v = np.mgrid[0:2 * np.pi:30j, 0:np.pi:20j]
x = np.cos(u) * np.sin(v)
y = np.sin(u) * np.sin(v)
z = np.cos(v)

# Plot 3D wireframe
ax.plot_wireframe(x, y, z, color="red")
ax.set_title("Interactive 3D Sphere")

# Add axis labels
ax.set_xlabel('X axis')
ax.set_ylabel('Y axis')
ax.set_zlabel('Z axis')

plt.show()

Alternative Surface Plot

You can also create interactive surface plots with colors ?

import matplotlib.pyplot as plt
import numpy as np

# Create data for surface plot
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))

# Create 3D surface plot
fig = plt.figure(figsize=(10, 7))
ax = fig.add_subplot(111, projection='3d')

surface = ax.plot_surface(X, Y, Z, cmap='viridis', alpha=0.8)
ax.set_title("Interactive 3D Surface Plot")
ax.set_xlabel('X axis')
ax.set_ylabel('Y axis')
ax.set_zlabel('Z axis')

# Add color bar
fig.colorbar(surface)
plt.show()

Key Interactive Features

Action Mouse Control Description
Rotate Left click + drag Rotate the 3D plot around its center
Zoom Right click + drag Zoom in/out of the plot
Pan Middle click + drag Pan the plot in different directions

Tips for Better Interactivity

For optimal performance and appearance ?

  • Use %matplotlib widget for newer Jupyter environments with ipywidgets

  • Set appropriate figure size with figsize=(width, height)

  • Add proper axis labels and titles for better visualization

  • Use alpha parameter for transparency in surface plots

Conclusion

Interactive 3D plots in Jupyter Notebook enhance data visualization by allowing real-time manipulation. Use %matplotlib notebook or %matplotlib widget to enable interactivity, then create 3D plots with projection='3d' for an engaging visualization experience.

Updated on: 2026-03-25T18:00:19+05:30

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements