How to save Matplotlib 3d rotating plots?

To save Matplotlib 3D rotating plots, we can create an animated sequence and save it as a GIF or video file. This involves rotating the 3D plot through different angles and capturing each frame.

Basic Setup

First, let's create a basic 3D plot that we'll rotate ?

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

# Set figure size and layout
plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True

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

# Get test data and create wireframe
X, Y, Z = axes3d.get_test_data(0.1)
ax.plot_wireframe(X, Y, Z, rstride=5, cstride=5)

plt.show()

Method 1: Saving Individual Frames

Save each rotation angle as a separate image file ?

from mpl_toolkits.mplot3d import axes3d
import matplotlib.pyplot as plt
import os

# Create directory for frames
os.makedirs('frames', exist_ok=True)

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

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
X, Y, Z = axes3d.get_test_data(0.1)
ax.plot_wireframe(X, Y, Z, rstride=5, cstride=5)

# Save frames at different angles
for angle in range(0, 360, 10):
    ax.view_init(30, angle)
    plt.draw()
    plt.savefig(f'frames/frame_{angle:03d}.png', dpi=100, bbox_inches='tight')
    print(f"Saved frame at angle {angle}")

plt.close()
print("All frames saved successfully!")

Method 2: Creating Animated GIF

Use matplotlib's animation module to create and save an animated GIF ?

from mpl_toolkits.mplot3d import axes3d
import matplotlib.pyplot as plt
import matplotlib.animation as animation

def rotate_plot(angle, ax, X, Y, Z):
    ax.clear()
    ax.plot_wireframe(X, Y, Z, rstride=5, cstride=5)
    ax.view_init(30, angle)
    ax.set_title(f'3D Plot - Angle: {angle}°')

plt.rcParams["figure.figsize"] = [8, 6]
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
X, Y, Z = axes3d.get_test_data(0.1)

# Create animation
angles = range(0, 360, 5)
ani = animation.FuncAnimation(fig, rotate_plot, frames=angles, 
                             fargs=(ax, X, Y, Z), interval=100, repeat=True)

# Save as GIF (requires pillow: pip install pillow)
ani.save('rotating_3d_plot.gif', writer='pillow', fps=10)
print("Animated GIF saved as 'rotating_3d_plot.gif'")

Method 3: Saving as MP4 Video

Save the rotation as an MP4 video file ?

from mpl_toolkits.mplot3d import axes3d
import matplotlib.pyplot as plt
import matplotlib.animation as animation

def animate_rotation(frame, ax, X, Y, Z):
    angle = frame * 2  # 2 degrees per frame
    ax.view_init(30, angle)
    return ax,

plt.rcParams["figure.figsize"] = [10, 8]
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
X, Y, Z = axes3d.get_test_data(0.05)
ax.plot_wireframe(X, Y, Z, rstride=3, cstride=3, color='blue', alpha=0.7)
ax.set_title('Rotating 3D Wireframe Plot')

# Create animation with 180 frames (360° rotation)
ani = animation.FuncAnimation(fig, animate_rotation, frames=180, 
                             fargs=(ax, X, Y, Z), interval=50, blit=False)

# Save as MP4 (requires ffmpeg)
ani.save('rotating_plot.mp4', writer='ffmpeg', fps=20, bitrate=1800)
print("Video saved as 'rotating_plot.mp4'")

Comparison of Methods

Method Output Format File Size Quality Requirements
Individual Frames PNG images Large High Basic matplotlib
Animated GIF GIF Medium Good pillow package
MP4 Video MP4 Small Excellent ffmpeg

Conclusion

Use individual frames for maximum control, animated GIFs for web compatibility, or MP4 videos for the best quality-to-size ratio. The animation module provides the most professional results for rotating 3D plots.

Updated on: 2026-03-25T23:03:45+05:30

6K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements