Advertisement
furas

matplotlib - animation sinus and points #2

Apr 26th, 2017
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.94 KB | None | 0 0
  1. #!/usr/bin/env python3
  2.  
  3. # animated GIF: https://media.giphy.com/media/3og0IR8kl9YxONfB7y/giphy.gif
  4.  
  5. import numpy as np
  6. import matplotlib.pyplot as plt
  7. import matplotlib.animation as animation
  8.  
  9. fig, ax = plt.subplots()
  10.  
  11. x = np.arange(0, 2*np.pi, 0.1)
  12. px = np.arange(0, 2*np.pi, (2*np.pi-0.1)/4)
  13.  
  14. #q = len(x)//4
  15. #px = np.array([x[q*0], x[q*1], x[q*2], x[q*3], x[q*4]])
  16.  
  17. line, = ax.plot(x, np.sin(x), 'red')
  18. points, = ax.plot(px, np.sin(px), 'go') # 'g' = green, 'o' = circles
  19.  
  20. def animate(i):
  21.     print(i)
  22.     line.set_ydata(np.sin(x+i))
  23.     points.set_ydata(np.sin(px+i))
  24.     return line, points
  25.  
  26. # interval=50 gives fps=20 (50ms*20 = 1000ms = 1second)
  27. ani = animation.FuncAnimation(fig, animate, x, interval=50, blit=True)
  28.  
  29. # display animation - it doesn't stop - you have to close window
  30. plt.show()
  31.  
  32. # save in file - it may need some time to render and save all frames
  33. ani.save('animation.gif', writer='imagemagick', fps=20)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement