如何在不显示视频中以前的帧的情况下保存动画?

326

我想使用Python保存动画,但是我得到了叠加的帧!我想单独显示框架。请在这里我用过的:

import numpy as np
from matplotlib import pyplot as plt
from matplotlib import animation
from numpy import pi, cos, sin

fig = plt.figure()
plt.axis([-1.5, 1.5,-1.5, 1.5])
ax = plt.gca()
ax.set_aspect(1)

N=100

xp = [None] * N
yp = [None] * N

def init():
    # initialize an empty list of cirlces
    return []

def animate(i):

    xp[i]=sin(i*pi/10)
    yp[i]=cos(i*pi/10)

    patches = []

    patches.append(ax.add_patch( plt.Circle((xp[i],yp[i]),0.02,color='b') ))
    return patches 

anim = animation.FuncAnimation(fig, animate, init_func=init,
                               frames=N-1, interval=20, blit=True)

anim.save("example.avi")
plt.show()
神户约翰

有些事情我不确定,实际上似乎是axis.plot()行为和FuncAnimate()行为是不同的。但是,下面的代码对两者都适用。

仅使用一个补丁(针对您的情况)

您的代码的关键点在于,每次迭代时,除了旧的圆之外,您还添加了一个新的圆:

patches = []
patches.append(ax.add_patch( plt.Circle((xp[i],yp[i]),0.02,color='b') ))

即使清除了修补程序列表,它们仍存储在轴中。

相反,只需创建一个圆并更改其位置即可。

用清除第一帧 init()

另外,init()需要从底架上清除补丁。

独立示例

from matplotlib import pyplot as plt
from matplotlib import animation
from numpy import pi, cos, sin

fig = plt.figure()
plt.axis([-1.5, 1.5, -1.5, 1.5])
ax = plt.gca()
ax.set_aspect(1)
N = 100
xp = []
yp = []


# add one patch at the beginning and then change the position
patch = plt.Circle((0, 0), 0.02, color='b')
ax.add_patch(patch)
def init():
    patch.set_visible(False)
    # return what you want to be cleared when axes are reset
    # this actually clears even if patch not returned it so I'm not sure
    # what it really does
    return tuple()


def animate(i):
    patch.set_visible(True)  # there is probably a more efficient way to do this
    # just change the position of the patch
    x, y = sin(i*pi/10), cos(i*pi/10)
    patch.center = x, y
    # I left this. I guess you need a history of positions.
    xp.append(x)
    yp.append(y)
    # again return what you want to be cleared after each frame
    # this actually clears even if patch not returned it so I'm not sure
    # what it really does
    return tuple()


anim = animation.FuncAnimation(fig, animate, init_func=init,
                               frames=N-1, interval=20, blit=True)
# for anyone else, if you get strange errors, make sure you have ffmpeg
# on your system and its bin folder in your path or use whatever
# writer you have as: writer=animation.MencoderWriter etc...
# and then passing it to save(.., writer=writer)
anim.save('example.mp4')
plt.show()

返回值???

关于返回值init()animate(),这似乎并不重要返回什么。单个补丁仍然可以移动并正确绘制,而无需清除先前的补丁。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在不显示的情况下编辑保存在.fig文件中的图形的属性

如何在不显示保存对话框的情况下保存Excel文件

如何在不显示 SaveDialog 的提示的情况下将文件保存在特定位置

如何在 OpenCV 中不增加大小的情况下将视频分成帧

如何在不显示文档的情况下编写段落

如何在不显示输出的情况下运行链接

如何在不暴露代码的情况下显示动画?

如何在不显示Flask中的URI字段的情况下进行重定向?

如何在不显示 dtype 的情况下从对象在 Python 中打印?

如何在不显示dm脚本中的图像的情况下添加比例尺

如何在不复制帧的情况下提高视频速度和帧速率

如何在不循环使用关键帧动画的情况下更改内容

如何在不显示Windows登录窗口的情况下从远程桌面连接中的会话注销?

在Django中,如何在不显示链接的情况下将信息传递到服务器?

如何在Google地图中不显示标记图标的情况下显示信息窗口

如何在不显示/显示的情况下打印Windows窗体

如何在不显示开始菜单的情况下显示任务栏?

MPAndroidChart:如何在不显示Y轴的情况下显示极限线?

如何在不显示垂直滚动条的情况下显示标题?

如何在不将张量保存到磁盘的情况下将张量恢复为以前的值?

如何在不显示Windows控制台的情况下运行Python脚本

如何在不显示终端窗口的情况下运行Cygwin程序(xemacs)?

发送后如何在不显示推送消息的情况下运行该功能?

如何在不显示所选值的情况下创建下拉按钮

如何在不显示php的情况下强制使用php和ajax下载图像

如何在不显示URL的情况下将参数传递给Angularjs的router-ui?

如何在不显示多个图标的情况下添加多个悬停工具?

如何在不显示查询结果的情况下测量查询持续时间?

如何在不显示InCall屏幕的情况下阻止呼叫