Last active
November 6, 2018 08:43
-
-
Save rishi93/497821836e106300959d87a58b586a22 to your computer and use it in GitHub Desktop.
Animated 3D plot in Matplotlib
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
import matplotlib.pyplot as plt | |
from mpl_toolkits import mplot3d | |
from matplotlib.animation import FuncAnimation | |
# Create the figure and the axes | |
fig = plt.figure() | |
ax = fig.add_subplot(111, projection = '3d', title = '3D curve animation') | |
# Initialize the axes | |
ax.set_xlim(-1, 1) | |
ax.set_ylim(-1, 1) | |
ax.set_zlim(-4*np.pi, 4*np.pi) | |
# Prepare the datapoints | |
z = np.linspace(-4*np.pi, 4*np.pi, 100) | |
x = np.sin(z) | |
y = np.cos(z) | |
bundled_data = [(x[i], y[i], z[i]) for i in range(0, 100)] | |
x_data, y_data, z_data = [], [], [] | |
# Plot the datapoints | |
lines = ax.plot(x_data, y_data, z_data, animated = True) | |
# Setup the animation | |
def update(frame): | |
x_point, y_point, z_point = frame | |
x_data.append(x_point) | |
y_data.append(y_point) | |
z_data.append(z_point) | |
lines[0].set_data(x_data, y_data) | |
lines[0].set_3d_properties(z_data) | |
return lines | |
animation = FuncAnimation(fig, update, frames = bundled_data, blit = True, interval = 40) | |
# Display the plot | |
plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment