Path: blob/master/JSAnimation/template/make_lorenz_frames.py
934 views
import numpy as np1from scipy import integrate23from matplotlib import pyplot as plt4from mpl_toolkits.mplot3d import Axes3D5from matplotlib.colors import cnames6from matplotlib import animation78N_trajectories = 2091011def lorentz_deriv((x, y, z), t0, sigma=10., beta=8./3, rho=28.0):12"""Compute the time-derivative of a Lorentz system."""13return [sigma * (y - x), x * (rho - z) - y, x * y - beta * z]141516# Choose random starting points, uniformly distributed from -15 to 1517np.random.seed(1)18x0 = -15 + 30 * np.random.random((N_trajectories, 3))1920# Solve for the trajectories21t = np.linspace(0, 4, 1000)22x_t = np.asarray([integrate.odeint(lorentz_deriv, x0i, t)23for x0i in x0])2425# Set up figure & 3D axis for animation26fig = plt.figure()27ax = fig.add_axes([0, 0, 1, 1], projection='3d')28ax.axis('off')2930# choose a different color for each trajectory31colors = plt.cm.jet(np.linspace(0, 1, N_trajectories))3233# set up lines and points34lines = sum([ax.plot([], [], [], '-', c=c)35for c in colors], [])36pts = sum([ax.plot([], [], [], 'o', c=c)37for c in colors], [])3839# prepare the axes limits40ax.set_xlim((-25, 25))41ax.set_ylim((-35, 35))42ax.set_zlim((5, 55))4344# set point-of-view: specified by (altitude degrees, azimuth degrees)45ax.view_init(30, 0)4647# initialization function: plot the background of each frame48def init():49for line, pt in zip(lines, pts):50line.set_data([], [])51line.set_3d_properties([])5253pt.set_data([], [])54pt.set_3d_properties([])55return lines + pts5657# animation function. This will be called sequentially with the frame number58def animate(i):59# we'll step two time-steps per frame. This leads to nice results.60i = (2 * i) % x_t.shape[1]6162for line, pt, xi in zip(lines, pts, x_t):63x, y, z = xi[:i].T64line.set_data(x, y)65line.set_3d_properties(z)6667pt.set_data(x[-1:], y[-1:])68pt.set_3d_properties(z[-1:])6970ax.view_init(30, 0.3 * i)71fig.canvas.draw()72return lines + pts7374# instantiate the animator.75#anim = animation.FuncAnimation(fig, animate, init_func=init,76# frames=500, interval=30, blit=True)7778# Save as mp4. This requires mplayer or ffmpeg to be installed79#anim.save('lorentz_attractor.mp4', fps=15, extra_args=['-vcodec', 'libx264'])8081#plt.show()8283for i in range(100):84animate(2 * i + 1)85fig.savefig('frames/frame%.03i.png' % (i + 1), dpi=80)86878889