欢迎您访问 最编程 本站为您分享编程语言代码,编程技术文章!
您现在的位置是: 首页

绘制动态正弦曲线

最编程 2024-04-22 10:11:00
...
%matplotlib notebook 
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
 
# 0. 设置中文黑体
plt.rcParams["font.sans-serif"] = ["SimHei"]
plt.rcParams["axes.unicode_minus"] = False
# 1. 准备正弦曲线数据
x = np.linspace(0, 2*np.pi, 100)
y = np.sin(x)
 
# 2. 创建坐标系
fig = plt.figure(tight_layout=True)  # fig表示动画所在画布
ax = fig.add_subplot(111)
 
# 3. 绘制正弦曲线
ax.plot(x, y)
# 4. 动画设计
def update_points(num):
    point_ani.set_data(x[num], y[num])    # num代表当前动画第几帧
    text_pt.set_text("x=%.3f, y=%.3f"%(x[num], y[num]))
    return point_ani,text_pt,
 
x = np.linspace(0, 2*np.pi, 100)
y = np.sin(x)
 
plt.plot(x,y, color='blue')
point_ani, = plt.plot(x[0], y[0], "ro")
text_pt = plt.text(4, 0.8, '', fontsize=16)
 
# # 开始制作动画
ani = animation.FuncAnimation(fig, update_points, np.arange(0, 100), interval=100, blit=True)
# # 5.将动画保存为gif图片(用于上传博客)
ani.save("pratice7.3.gif", writer='pillow') 
 
# 6.展示图表
plt.show()