import matplotlib.pyplot as plt
import matplotlib.animation as animation
# 数据准备
x = [1, 2, 3, 4, 5]
dist_val = [1, 4, 9, 16, 25]
points_x = [2, 4]
points_y = [4, 16]
# 创建一个新的Figure和轴对象
fig, ax = plt.subplots()
# 定义绘图函数,用于更新动画帧
def animate(i):
ax.clear() # 清除轴对象内容
ax.plot(x[:i+1], dist_val[:i+1], color='blue') # 绘制部分线条
ax.scatter(points_x[:i+1], points_y[:i+1], color="green") # 绘制部分散点
ax.plot(points_x[:i+1], points_y[:i+1], color='red') # 绘制部分线条
# 创建动画对象
ani = animation.FuncAnimation(fig, animate, frames=len(x), interval=500)
# 保存为GIF动图
ani.save('animation.gif', writer='imagemagick')
使用matplotlib保存gif动图
最新推荐文章于 2024-10-10 08:34:12 发布