python matplotlib 绘制sinx动画
"""
绘制sin函数的动画
"""
import numpy as np
import matplotlib.pyplot as mp
import matplotlib.animation as ma
x_t = np.linspace(0, 2 * np.pi, 500)
y = np.sin(x_t)
mp.figure("Sin(x) Animation", facecolor="lightgray")
mp.title("Sin(x) Animation", fontsize=18)
mp.xlim(0, 2 * np.pi + 1)
mp.ylim(-1.1, 1.1)
mp.xticks([0, np.pi / 2, np.pi, 3 * np.pi / 2, 2 * np.pi],
["0", r"$\frac{\pi}{2}$", r"$\pi$", r"$\frac{3\pi}{2}$", r"$2\pi$"])
ax = mp.gca()
ax.spines["top"].set_color("none")
ax.spines["right"].set_color("none")
ax.spines["bottom"].set_position(("data", 0))
pl = mp.plot([], [], c="r")[0]
def update(number):
index = number % 500
pl.set_data(x_t[:index], y[:index])
anim = ma.FuncAnimation(mp.gcf(), update, interval=10)
mp.grid(linestyle=":")
mp.show()