Moving Point Animation

##############################################################
## Animating a point moving on a curve
## y = sin(x) as an example
##############################################################

import numpy as np
# matplotlib.use("Agg")
import matplotlib.pyplot as plt
import matplotlib.animation as animation
from math import pi


def update_line(num, data, line):
    line.set_data(data[..., :num])
    return line,


N = 1000; # data size
x = np.linspace(0,2.0*pi,N)
y = np.sin(x)

plt.rcParams["font.family"] = "Times New Roman"
fig = plt.figure(figsize=(3.14961, 2.3622), dpi=450)
# plt.plot(x, y, label='y = sin(x)')
plt.xlim(0, 2.0*pi)
plt.ylim(-1.2, 1.2)
plt.xticks(fontsize=10)
plt.yticks(fontsize=10)
legend_font = {
    'family': 'Times New Roman',  # 字体
    'style': 'normal',
    'size': 10,  # 字号
    'weight': "normal",  # 是否加粗,不加粗
}
plt.legend(loc='best', framealpha=0, prop=legend_font)
plt.xlabel('X', fontsize=12, fontweight='bold')
plt.ylabel('Y', fontsize=12, fontweight='bold')
plt.tight_layout()
# # plt.show()

data = np.zeros((2, len(x)))
data[0, :] = x
data[1, :] = y
l, = plt.plot([], [], 'r')
line_ani = animation.FuncAnimation(fig, update_line, N, fargs=(data, l),
                                   interval=1, blit=True)
#
# Set up formatting for the movie files
video_length = 10 # seconds
Writer = animation.writers['ffmpeg']
writer = Writer(fps=N/video_length, metadata=dict(artist='Me'), bitrate=1800)
line_ani.save('sinx.mp4', writer=writer)