0

I get different plots for each value that I wrote in for loop's upper range as I expected. But I would like to animate the plot from range (0,0) an goes to (0,15) as the upper limit changes 1 by 1, by using matplotlib animation function animation.FuncAnimation(). So there will be 16 frames total in the animation. I messed up with the animation part, so I'm pasting the code that gives 1 plot output. Thanks in advance!

import numpy as np import matplotlib.pyplot as plt import matplotlib.animation as animation g = 1.0 def distance(x1,y1,x2,y2): r = np.sqrt((x2-x1)**2+(y2-y1)**2) return r def gravit(m1,m2,r): f = g*m1*m2/(r**2) return f def angle(y2, y1, x2, x1): ydif = y1-y2 xdif = x1-x2 angle = np.arctan2(ydif,xdif) return angle m1 = 100 x1, y1 = 0,0 m2 = 1 x2, y2 = -15,-10 vx1 = 1 vy1 = 0 ax1 = 0 ay1 = 0 vx2 = 2 vy2 = 3.9 ax2 = 0 ay2 = 0 x1coor = [x1] y1coor = [y1] x2coor = [x2] y2coor = [y2] for t in range(0,10): #This value of 10 should be change from 0 to 15 in the animation r = distance(x1,y1,x2,y2) fx1 = gravit(m1, m2, r) * np.cos(angle(y2,y1,x2,x1)) fy1 = gravit(m1, m2, r) * np.sin(angle(y2,y1,x2,x1)) ax2 = fx1/m2 vx2 = vx2 + ax2 ay2 = fy1/m2 vy2 = vy2 + ay2 x2 = x2 + vx2 + 0.5*ax2 y2 = y2 + vy2 + 0.5*ay2 x1 = x1 + vx1 + 0.5*ax1 y1 = y1 + vy1 + 0.5*ay1 x1coor.append(x1) y1coor.append(y1) x2coor.append(x2) y2coor.append(y2) plt.axes().set_aspect('equal') plt.axis([-30,30,-30,30]) plt.plot(x1coor,y1coor, '-.', color='blue') plt.plot(x2coor,y2coor, '-.', color='black') plt.scatter(x1,y1,s=m1*20, color='blue') plt.scatter(x2,y2,s=m2*20, color='red') 
0

1 Answer 1

2

The FuncAnimation function of matplotlib's animation module requires a figure and a function to draw each frame, so first initialize your figure following :

fig = plt.figure() ax1 = fig.add_subplot(1, 1, 1) ax1.set_aspect("equal") ax1.set_xlim(-30, 30) ax1.set_ylim(-30, 30) l1, = ax1.plot([], [], linestyle = "-.", color = "blue") l2, = ax1.plot([], [], '-.', color = "black") s1, = ax1.plot([], [], linestyle = None, marker = "o", markersize = 5, color = "blue") s2, = ax1.plot([], [], linestyle = None, marker = "o", markersize = 5, color = "red") 

Note that the different line plots contain no input data and are saved in the variables l1, l2, s1 and s2.

Now define the function that will be called to draw each frame. The first argument is always the frame number. This function should return all the plot objects that are updated using their methods 'set_data':

def update_fig(i, x1coor, y1coor, x2coor, y2coor): l1.set_data(x1coor[:i+1], y1coor[:i+1]) l2.set_data(x2coor[:i+1], y2coor[:i+1]) s1.set_data(x1coor[i], y1coor[i]) s2.set_data(x2coor[i], y2coor[i]) return l1, l2, s1, s2, 

You can now run your animation (note that 'update_fig' arguments, except 'i', are passed to the function using the keyword 'fargs'):

ani = animation.FuncAnimation(fig, update_fig, frames = len(x1coor), fargs = (x1coor, y1coor, x2coor, y2coor), interval = 100, repeat = True, ) 
Sign up to request clarification or add additional context in comments.

1 Comment

That was exactly what I mean, thank you for explaining and teaching it :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.