绘制动态正弦曲线
最编程
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()
上一篇: 正弦函数公式的基本推导
下一篇: 用 xml 编写动画的详细示例
推荐阅读
-
动态三维重建的快速地图序列生成方法
-
动力扣爆发刷新动态规划五连环的第 129 天(完美方格、单词拆分和劫持房子)
-
华为 OD 机测试 - 跳格 3 - 动态编程(Java 2024 C 卷 200 分) - III.
-
逼真的实时渲染:基于街道高斯的动态城市场景建模
-
第八届高校自主招生综合评价(三位一体)招生动态 浙江分论坛暨中学尖子生培养学科骨干教师培训
-
问题解决思路的贪婪算法、回溯算法和动态编程分析 - 决策方法
-
ThreeJs 绘制点、线和面
-
在 shape.xml 中动态更改图形颜色的 Android 实现
-
对贝尔曼优化原理和动态编程的个人理解
-
[计算机绘图]绘制填充模型:重心坐标、Z 缓冲区