fork download
  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3. from matplotlib.animation import FuncAnimation
  4.  
  5. L = 2
  6. f1 = 1
  7. f2 = 1.5
  8. A1 = 1
  9. A2 = 0.8
  10.  
  11. fig = plt.figure(figsize=(7,7))
  12. ax1 = fig.add_subplot(2,1,1,xlim = (-3,3),ylim = (-3,1))
  13. line1, = ax1.plot([],[],'y-', lw = 2)
  14. line2, = ax1.plot([],[],'b-', lw = 2)
  15. bob1, = ax1.plot([],[],'oy')
  16. bob2, = ax1.plot([],[],'ob')
  17.  
  18. ax2 = fig.add_subplot(2,1,2, xlim = (0,10), ylim = (-1.5, -1.5))
  19. t_data = np.linspace(0,10,500)
  20. py, = ax2.plot([],[],'y-')
  21. pb, = ax2.plot([],[],'b-')
  22.  
  23. def init():
  24. line1.set_data([],[])
  25. line2.set_data([],[])
  26. bob1.set_data([],[])
  27. bob2.set_data([],[])
  28. py.set_data([],[])
  29. pb.set_data([],[])
  30. return line1, line2, bob1, bob2, py, pb
  31.  
  32. def update(i):
  33. t = i/50
  34. th1 = A1*np.cos(2*np.pi*f1*t)
  35. th2 = A2*np.cos(2*np.pi*f2*t + np.pi/4)
  36.  
  37. x1 = L*np.sin(th1)
  38. y1 = L*np.cos(th1)
  39. x2 = L*np.sin(th2)
  40. y2 = L*np.cos(th2)
  41.  
  42. line1.set_data([0, x1],[0, y1])
  43. line2.set_data([0, x2], [0, y2])
  44. bob1.set_data([x1],[y1])
  45. bob2.set_data([x2], [y2])
  46.  
  47. idx = np.searchsorted(t_data,t)
  48. py.set_data(t_data[:idx], A1*np.cos(2*np.pi*f1*t_data[:idx]))
  49. pb.set_data(t_data[:idx], A2*np.cos(2*np*f2*t_data[:idx] + np.pi/4))
  50.  
  51. if t>8: ax2.set_xlim(t - 8, t + 2)
  52. return line1, line2, bob1, bob2, py, pb
  53.  
  54. ani = FuncAnimation(fig, update, frames = 500, init_func = init, blit = True, interval = 20)
  55. plt .tight_layout()
  56. plt.show()
Success #stdin #stdout #stderr 3.73s 69564KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Fontconfig error: No writable cache directories
./prog.py:18: UserWarning: Attempting to set identical low and high ylims makes transformation singular; automatically expanding.