|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 Asmendeus 于 2021-12-23 23:39 编辑
函数是在管口处x方向流出,y方向上做正弦振动,在z方向上受重力的水流
问题是 对于第n帧的动画,第n帧以前的所有图像都被保留了下来,但是我希望它在第n帧时只显示第n帧的图像
- import builtins
- from matplotlib import animation
- import numpy as np
- import matplotlib.pyplot as plt
- from mpl_toolkits.mplot3d import Axes3D
- from matplotlib.animation import FuncAnimation
- #时间单位ms, 长度单位mm
- #t大概在500ms内, z大概在900mm内, x大概在300mm内, y大概在100mm内
- #运动参数
- Vx0 = 0.75 #管口水在x方向的速率
- Vy0 = 0.25 #管口水在y方向最大的速率
- f = 30 #振动频率
- w = 2*np.pi * f / 1000 #将f转化为圆频率
- g = 9.8 * 10**(-3) #重力加速度
- t0 = 1000 #t0为拍照时刻,1秒后水流稳定再开始拍照
- #初始化
- fig = plt.figure()
- ax1 = Axes3D(fig)
- plt.xlim((0,400))
- plt.ylim((-300,300))
- ax1.set_zlim(-1000,0)
- #函数图像
- def get_line():
- global t0
- t0 += 1000/29
- #每1/30秒取样一次
- t = np.linspace(0,1000,500, endpoint=False)
- #t用来生成轨迹, 是从管口出发运动到此拍照时刻的时间
- x = Vx0 * t
- y = - Vy0 * np.sin(w*(t0-t)) * t + Vy0 * np.cos(w*(t0-t)) / w
- z = - g * t**2 / 2
- line = np.stack((x,y,z),axis=1)
- print(line)
- return line
- def update(i):
- label = 'timestep {0}'.format(i)
- ax1.set_xlabel(label)
- line = get_line()
- ax1.plot3D(line[:, 0], line[:, 1], line[:, 2], 'blue')
- return ax1
- anim = FuncAnimation(fig, update, frames=np.arange(0, 50), interval=2, blit=False, repeat=False)
- plt.show()
复制代码 |
|