在python中创建具有不同时间间隔的动画

2024-04-19 17:05:53 发布

您现在位置:Python中文网/ 问答频道 /正文

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt 
import matplotlib.animation as animation
fig, ax = plt.subplots(figsize=(3,9))
dot, = ax.plot([], [], 'ro')

def init():
    ax.set_xlim(0,1)
    ax.set_ylim(0,3)
    return

def gen_dot():
    for i in range(len(list_x_y)):
    newdot = list_x_y[i]
    yield newdot

def update_dot(newd):
    dot.set_data(newd[0],newd[1])
    return dot

ani = animation.FuncAnimation(fig, update_dot, frames = gen_dot, interval = 100, 
init_func=init)
ani.save('Steps.gif',writer='imagemagick',fps=24)
plt.show()

嘿,伙计们,所以我试图创建一个动画模仿一个真实的人走在跑步机与代码如上所示。问题是,我需要一个可变的时间间隔,这样的人不是在一个恒定的速度,这是不现实的。你知道吗


我想,可能动画功能不支持不同的时间间隔功能,所以我尝试使用For循环,并获得一个时间间隔列表,以便每次在For循环中,它将睡眠一个特定的时间量,并继续…但是,JupyterNotebook不会刷新帧,因为我打算它,创建一个静态图像在这段时间的最后…有没有什么方法我可以创建一个不同时间间隔的gif?我想也许我可以保存所有的帧并使用imageio创建一个gif…但是imageio是否也需要不同的时间间隔作为输入?我为这个问题挣扎了好几个小时…任何帮助都将不胜感激。谢谢!你知道吗


Tags: import间隔matplotlibinitdefas时间fig