I am trying to animate a 2D circular orbit around the origin. I got the below code working but the trailing line “blinks” everytime it crosses the positive x-axis (completes a full rotation). I understand that this happens when I re-set the data inside the animate function, but I have no idea how to fix this. Is it possible to implement smoothly?
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.animation as animation
fig, ax = plt.subplots(figsize=(6,6))
ax.set_xlim(-20, 20)
ax.set_ylim(-20, 20)
ax.axhline(0, color='black', linewidth=0.5)
ax.axvline(0, color='black', linewidth=0.5)
rads = np.arange(0, 2*np.pi, 0.01)
x = 5*np.cos(rads)
y = 5*np.sin(rads)
point, = ax.plot(5, 0, color='red', marker="o")
line, = ax.plot([], [], color='red')
def animate(i):
speed = 1000
# arg - used in resetting the orbit
arg = (i - (2*np.pi*int(i/((2*np.pi)*(100/speed))))*(100/speed))*speed
line.set_data(x[int(arg):int(arg)+50], y[int(arg):int(arg)+50])
point.set_data([5*np.cos(i*(speed/100)-100)],[5*np.sin(i*(speed/100)-100)])
return line, point,
ani = animation.FuncAnimation(
fig, animate, frames=np.linspace(0,2*np.pi,int(2*np.pi/0.01), endpoint=False),interval=20, blit=True)
plt.show()