I want to plot a moving dot from left to right. Here's my code:
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.animation import FuncAnimation
Acc_11 = [0,1,2,3,4,5,6,7,8]
Acc_12 = [4,4,4,4,4,4,4,4,4]
fig = plt.figure()
axes = fig.add_subplot(111, autoscale_on=False)
axes.set_xlim(min(Acc_11), max(Acc_11))
axes.set_ylim(0, 8)
point, = axes.plot([Acc_11[0]],[Acc_12[0]], 'go')
def ani(coords):
point.set_data([coords[0]],[coords[1]])
return point,
def frames():
for acc_11_pos, acc_12_pos in zip(Acc_11, Acc_12):
yield acc_11_pos, acc_12_pos
ani = FuncAnimation(fig, ani, frames=frames, interval=300)
plt.show()
However, the dot stops at each point then continue, but I want the dot moving smoothly in this speed without changing the interval
. Can anyone please help?
CodePudding user response:
"Smooth" would always require "more frames" in my opinion. So I do not see a way to make the movement smoother, i.e. increase the number of frames, without increasing the frames per second, i.e. changing the interval.
Here's a version with frames increased tenfold and interval reduced tenfold:
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.animation import FuncAnimation
Acc_11 = np.linspace(0,8,90) # increased frames
Acc_12 = np.ones(len(Acc_11))*4
fig = plt.figure()
axes = fig.add_subplot(111, autoscale_on=False)
axes.set_xlim(min(Acc_11), max(Acc_11))
axes.set_ylim(0, 8)
point, = axes.plot([Acc_11[0]],[Acc_12[0]], 'go')
def ani(coords):
point.set_data([coords[0]],[coords[1]])
return point,
def frames():
for acc_11_pos, acc_12_pos in zip(Acc_11, Acc_12):
yield acc_11_pos, acc_12_pos
ani = FuncAnimation(fig, ani, frames=frames, interval=30) # decreased interval
plt.show()