Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import matplotlib
- import matplotlib.pyplot as plt
- import matplotlib.animation as animation
- from matplotlib import patches
- import numpy as np
- particle_one = np.zeros((10,2)) #10 times steps and x,y positions
- particle_two = np.zeros((10,2)) #10 times steps and x,y positions
- #the two particles are moving away from each other in the x direction
- for i in range(0,10):
- particle_one[i,0] = i
- particle_two[i,0] = 2-i
- particle_one[i,1] = 2
- particle_two[i,1] = -2
- particle_One_Radius = 1
- particle_Two_Radius = 1.5
- arc_Center = np.zeros((10,2))
- for i in range(0,10):
- arc_Center[i,0] = (particle_one[i,0] + particle_two[i,0])/2
- #the arc should disappear for frame 5
- arc_Center[5,0] = 0
- arc_Center[5,1] = 0
- #allows for bliting to occur correctly
- def _blit_draw(self, artists, bg_cache):
- # Handles blitted drawing, which renders only the artists given instead
- # of the entire figure.
- updated_ax = []
- for a in artists:
- # If we haven't cached the background for this axes object, do
- # so now. This might not always be reliable, but it's an attempt
- # to automate the process.
- if a.axes not in bg_cache:
- # bg_cache[a.axes] = a.figure.canvas.copy_from_bbox(a.axes.bbox)
- # change here
- bg_cache[a.axes] = a.figure.canvas.copy_from_bbox(a.axes.figure.bbox)
- a.axes.draw_artist(a)
- updated_ax.append(a.axes)
- # After rendering all the needed artists, blit each axes individually.
- for ax in set(updated_ax):
- # and here
- # ax.figure.canvas.blit(ax.bbox)
- ax.figure.canvas.blit(ax.figure.bbox)
- # MONKEY PATCH!!
- matplotlib.animation.Animation._blit_draw = _blit_draw
- #draws the figure
- fig = plt.figure()
- plt.axis([-20,20, -5,5]) #axis that I like
- ax = plt.gca()
- #defines the two circles
- circle_One = plt.Circle([particle_one[0,0],particle_one[0,1]],particle_One_Radius)
- circle_Two = plt.Circle([particle_two[0,0],particle_two[0,1]],particle_Two_Radius)
- circles = []
- circles.append(circle_One)
- circles.append(circle_Two)
- arcs = []
- #defines the arc
- arc_one = patches.Arc([arc_Center[0,0],arc_Center[0,1]],5,3,angle =0 ,theta1 = 0,theta2= 270)
- arcs.append(arc_one)
- def init():
- ax.add_patch(circles[0])
- ax.add_patch(circles[1])
- ax.add_patch(arcs[0])
- return ax
- #draw every frame by frame
- def animate(m):
- circles[0].center=((particle_one[m,0],particle_one[m,1]))
- circles[1].center=((particle_two[m,0],particle_two[m,1]))
- #the arcs does not change
- arcs[0] =patches.Arc([arc_Center[m,0],arc_Center[m,1]],5+m,3+m,angle =0 ,theta1 = 0,theta2= 270)
- return ax
- #animation function that draws 10 frames
- anim = animation.FuncAnimation(fig,animate, init_func= init, frames = 10, interval = 20)
- plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement