Guest User

Untitled

a guest
Jul 30th, 2024
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.56 KB | None | 0 0
  1. import numpy as np
  2. from scipy import integrate
  3.  
  4. from matplotlib import pyplot as plt
  5. from mpl_toolkits.mplot3d import Axes3D
  6. from matplotlib.colors import cnames
  7. from matplotlib import animation
  8.  
  9. N_trajectories = 20
  10.  
  11.  
  12. def lorentz_deriv(xyz, t0, sigma=10., beta=8./3, rho=28.0):
  13.     """Compute the time-derivative of a Lorentz system."""
  14.     x, y, z = xyz   # FIX
  15.     return [sigma * (y - x), x * (rho - z) - y, x * y - beta * z]
  16.  
  17.  
  18. # Choose random starting points, uniformly distributed from -15 to 15
  19. np.random.seed(1)
  20. x0 = -15 + 30 * np.random.random((N_trajectories, 3))
  21.  
  22. # Solve for the trajectories
  23. t = np.linspace(0, 4, 1000)
  24. x_t = np.asarray([integrate.odeint(lorentz_deriv, x0i, t)
  25.                   for x0i in x0])
  26.  
  27. # Set up figure & 3D axis for animation
  28. fig = plt.figure()
  29. ax = fig.add_axes([0, 0, 1, 1], projection='3d')
  30. ax.axis('off')
  31.  
  32. # choose a different color for each trajectory
  33. colors = plt.cm.jet(np.linspace(0, 1, N_trajectories))
  34.  
  35. # set up lines and points
  36. lines = sum([ax.plot([], [], [], '-', c=c)
  37.              for c in colors], [])
  38. pts = sum([ax.plot([], [], [], 'o', c=c)
  39.            for c in colors], [])
  40.  
  41. # prepare the axes limits
  42. ax.set_xlim((-25, 25))
  43. ax.set_ylim((-35, 35))
  44. ax.set_zlim((5, 55))
  45.  
  46. # set point-of-view: specified by (altitude degrees, azimuth degrees)
  47. ax.view_init(30, 0)
  48.  
  49. # initialization function: plot the background of each frame
  50. def init():
  51.     for line, pt in zip(lines, pts):
  52.         line.set_data([], [])
  53.         line.set_3d_properties([])
  54.  
  55.         pt.set_data([], [])
  56.         pt.set_3d_properties([])
  57.        
  58.     return lines + pts
  59.  
  60. # animation function.  This will be called sequentially with the frame number
  61. def animate(i):
  62.     # we'll step two time-steps per frame.  This leads to nice results.
  63.     i = (2 * i) % x_t.shape[1]
  64.  
  65.     for line, pt, xi in zip(lines, pts, x_t):
  66.         x, y, z = xi[:i].T
  67.         line.set_data(x, y)
  68.         line.set_3d_properties(z)
  69.  
  70.         pt.set_data(x[-1:], y[-1:])
  71.         pt.set_3d_properties(z[-1:])
  72.  
  73.     ax.view_init(30, 0.3 * i)
  74.     fig.canvas.draw()
  75.  
  76.     if False:  ### True stops animation but saves 1000 small .png files
  77.         fname = "Astro_Jake_" + str(i+10000)[1:]
  78.         fig.savefig(fname)
  79.  
  80.     return lines + pts
  81.  
  82. # instantiate the animator.
  83. anim = animation.FuncAnimation(fig, animate, init_func=init,
  84.                                frames=500, interval=30, blit=True)
  85.  
  86. # Save as mp4. This requires mplayer or ffmpeg to be installed
  87. #anim.save('lorentz_attractor.mp4', fps=15, extra_args=['-vcodec', 'libx264'])
  88.  
  89. plt.show()
  90.  
Advertisement
Add Comment
Please, Sign In to add comment