鱼C论坛

 找回密码
 立即注册
查看: 1828|回复: 2

matplotlib animation动画无法清除之前帧的图像

[复制链接]
发表于 2021-12-23 23:15:35 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 Asmendeus 于 2021-12-23 23:39 编辑

函数是在管口处x方向流出,y方向上做正弦振动,在z方向上受重力的水流
问题是 对于第n帧的动画,第n帧以前的所有图像都被保留了下来,但是我希望它在第n帧时只显示第n帧的图像

  1. import builtins
  2. from matplotlib import animation
  3. import numpy as np
  4. import matplotlib.pyplot as plt
  5. from mpl_toolkits.mplot3d import Axes3D
  6. from matplotlib.animation import FuncAnimation

  7. #时间单位ms, 长度单位mm
  8. #t大概在500ms内, z大概在900mm内, x大概在300mm内, y大概在100mm内

  9. #运动参数
  10. Vx0 = 0.75               #管口水在x方向的速率
  11. Vy0 = 0.25               #管口水在y方向最大的速率
  12. f = 30                   #振动频率
  13. w = 2*np.pi * f / 1000   #将f转化为圆频率
  14. g = 9.8 * 10**(-3)       #重力加速度
  15. t0 = 1000                #t0为拍照时刻,1秒后水流稳定再开始拍照

  16. #初始化
  17. fig = plt.figure()
  18. ax1 = Axes3D(fig)
  19. plt.xlim((0,400))
  20. plt.ylim((-300,300))
  21. ax1.set_zlim(-1000,0)

  22. #函数图像
  23. def get_line():
  24.     global t0
  25.     t0 += 1000/29
  26.     #每1/30秒取样一次
  27.     t = np.linspace(0,1000,500, endpoint=False)  
  28.     #t用来生成轨迹, 是从管口出发运动到此拍照时刻的时间
  29.     x = Vx0 * t
  30.     y = - Vy0 * np.sin(w*(t0-t)) * t + Vy0 * np.cos(w*(t0-t)) / w
  31.     z = - g * t**2 / 2
  32.     line = np.stack((x,y,z),axis=1)
  33.     print(line)
  34.     return line

  35. def update(i):

  36.     label = 'timestep {0}'.format(i)
  37.     ax1.set_xlabel(label)

  38.     line = get_line()
  39.     ax1.plot3D(line[:, 0], line[:, 1], line[:, 2], 'blue')
  40.     return ax1

  41. anim = FuncAnimation(fig, update, frames=np.arange(0, 50), interval=2, blit=False, repeat=False)
  42. plt.show()
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2021-12-23 23:30:38 | 显示全部楼层
37行line是后来用来测试x,y,z是否更新的,忘记删了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-12-23 23:38:26 | 显示全部楼层
已解决,在update里加了一个axe.clear()
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-30 18:10

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表