Advertisement
Guest User

python arc animate attempt

a guest
Aug 30th, 2015
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.83 KB | None | 0 0
  1. import matplotlib
  2. import matplotlib.pyplot as plt
  3. import matplotlib.animation as animation
  4. from matplotlib import patches
  5. import numpy as np
  6.  
  7.  
  8. particle_one = np.zeros((10,2)) #10 times steps and x,y positions
  9. particle_two = np.zeros((10,2)) #10 times steps and x,y positions
  10.  
  11.  
  12. #the two particles are moving away from each other in the x direction
  13. for i in range(0,10):
  14.     particle_one[i,0] = i
  15.     particle_two[i,0] = 2-i
  16.  
  17.     particle_one[i,1] = 2
  18.     particle_two[i,1] = -2
  19.    
  20.  
  21. particle_One_Radius = 1
  22. particle_Two_Radius = 1.5
  23.  
  24. arc_Center = np.zeros((10,2))
  25.  
  26. for i in range(0,10):
  27.     arc_Center[i,0] = (particle_one[i,0] + particle_two[i,0])/2
  28.  
  29.  
  30. #the arc should disappear for frame 5
  31. arc_Center[5,0] = 0
  32. arc_Center[5,1] = 0
  33.  
  34.  
  35. #allows for bliting to occur correctly
  36. def _blit_draw(self, artists, bg_cache):
  37.     # Handles blitted drawing, which renders only the artists given instead
  38.     # of the entire figure.
  39.     updated_ax = []
  40.     for a in artists:
  41.         # If we haven't cached the background for this axes object, do
  42.         # so now. This might not always be reliable, but it's an attempt
  43.         # to automate the process.
  44.         if a.axes not in bg_cache:
  45.             # bg_cache[a.axes] = a.figure.canvas.copy_from_bbox(a.axes.bbox)
  46.             # change here
  47.             bg_cache[a.axes] = a.figure.canvas.copy_from_bbox(a.axes.figure.bbox)
  48.         a.axes.draw_artist(a)
  49.         updated_ax.append(a.axes)
  50.  
  51.     # After rendering all the needed artists, blit each axes individually.
  52.     for ax in set(updated_ax):
  53.         # and here
  54.         # ax.figure.canvas.blit(ax.bbox)
  55.         ax.figure.canvas.blit(ax.figure.bbox)
  56.  
  57. # MONKEY PATCH!!
  58. matplotlib.animation.Animation._blit_draw = _blit_draw
  59.  
  60. #draws the figure
  61. fig = plt.figure()
  62. plt.axis([-20,20, -5,5]) #axis that I like
  63. ax = plt.gca()
  64.  
  65.  
  66. #defines the two circles
  67. circle_One = plt.Circle([particle_one[0,0],particle_one[0,1]],particle_One_Radius)
  68. circle_Two = plt.Circle([particle_two[0,0],particle_two[0,1]],particle_Two_Radius)
  69.  
  70. circles = []
  71.  
  72. circles.append(circle_One)
  73. circles.append(circle_Two)
  74.  
  75. arcs = []
  76. #defines the arc
  77. arc_one = patches.Arc([arc_Center[0,0],arc_Center[0,1]],5,3,angle =0 ,theta1 = 0,theta2= 270)
  78. arcs.append(arc_one)
  79.  
  80. def init():
  81.  
  82.     ax.add_patch(circles[0])
  83.     ax.add_patch(circles[1])
  84.     ax.add_patch(arcs[0])
  85.     return ax
  86.  
  87. #draw every frame by frame
  88. def animate(m):
  89.  
  90.    
  91.  
  92.  
  93.     circles[0].center=((particle_one[m,0],particle_one[m,1]))
  94.     circles[1].center=((particle_two[m,0],particle_two[m,1]))
  95.  
  96.     #the arcs does not change
  97.     arcs[0] =patches.Arc([arc_Center[m,0],arc_Center[m,1]],5+m,3+m,angle =0 ,theta1 = 0,theta2= 270)
  98.    
  99.     return ax
  100. #animation function that draws 10 frames
  101. anim = animation.FuncAnimation(fig,animate, init_func= init, frames = 10, interval = 20)
  102. plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement