Matplotlib动画不保存正确 [英] Matplotlib animation not saving properly

查看:511
本文介绍了Matplotlib动画不保存正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在该移动沿x轴的垂直线,在一系列的数据点的脚本。动画工作正常 plt.show ,但我有麻烦输出电影文件。请注意,我很新到Python,虽然我一直在玩弄它一年或两年。该脚本是通过将在本教程在回答psented脚本$ p $来这个previous堆栈溢出问题。该生产线最终会被移动通过静态数据线图,我在这里是presenting作为对角线。

影片文件具有正确的时间长度输出(1分钟,10秒),但线,应该从最左边每秒最右边,1点动,只移动了几个像素视频输出功能。

任何帮助您可以提供解决这一问题将是极大的AP preciated。

编辑:我在Ubuntu 14.04的Python运行2.7.6

下面是我的可重复code:

 导入numpy的是NP
从matplotlib进口pyplot如PLT
从matplotlib进口动画
进口时间#模拟背景资料
X = np.linspace(0,61,62)
Y = np.linspace(0,6,62)#设置我们想要的动画人物,轴,和剧情元素
y轴的MAX_HEIGHT = 6#最大高度
x轴n_pts = 61#最大长度#原始位置进度线
Y1 = [0,MAX_HEIGHT]
X1 = [0,0]图= plt.figure()#初始化图
#ax = fig.add_subplot(111)#Intialize轴
AX = plt.axes(XLIM =(0,n_pts),ylim =(0,MAX_HEIGHT))#组轴限制
行= ax.plot([],[],LW = 2)#初始化行#数据中得出的背景
行1,= ax.plot(X,Y​​,颜色='黑')#初始化函数:积每帧的背景
高清的init():
    line.set_data(X1,Y1)
    返回线,开始时间=选定了time.time()
mytimer = 0
mytimer_ref = 0#动画功能。这就是所谓的依次
高清动画(ⅰ):
    T =选定了time.time() - 开始时间
    mytimer = T + mytimer_ref
    X1 = [mytimer,mytimer]
    line.set_data(X1,Y1)
    返回线,#拨打动画。
动画= animation.FuncAnimation(图,动画,=的init_func初始化,
                               帧= 61,间隔= 1000)#保存动画为MP4。这需要的ffmpeg或男性codeR是
#安装。的extra_args确保的x264 codeC被使用,以便
#视频可以嵌入到HTML5。您可能需要调整本作
#您的系统:了解更多信息,请参阅
#http://matplotlib.sourceforge.net/api/animation_api.html作家= animation.writers ['的ffmpeg'](FPS = 1)anim.save('demo.mp4',作家=作家,DPI = DPI)plt.show()

修改下面的脚本创建电影并将其保存为MP4。现在的问题是,虽然有动画61帧,我不能让影片停在那里。它前进到每次100帧。我知道这个问题是有点老了,但任何帮助是极大的AP preciated!

我试图手动设定x轴,这限制了在屏幕上示出,但动画继续超出所示轴仍然。

 导入numpy的是NP
从matplotlib进口pyplot如PLT
从matplotlib进口动画
从时间导入时间
从scipy.interpolate进口interp1dy轴的MAX_HEIGHT = 6#最大高度
x轴n_pts = 62#最大长度X = np.linspace(0,61,62)
Y = np.linspace(0,6,62)白色背景#新图
图= plt.figure(facecolor ='白色')
AX = plt.axes(纵横= 1)#组轴限制#原始位置进度线
Y1 = [0,MAX_HEIGHT]
X1 = [0,0]#初始化行
plt.plot(X,Y​​)#plot背景
行= ax.plot([],[],LW = 2)DEF更新(帧):
    X1 =框架
    line.set_data(X1,Y1)    #返回修改后的行
    返回线,动画= animation.FuncAnimation(图,更新时间间隔= 1000,blit的= TRUE)
anim.save('line.mp4',作家='的ffmpeg')
plt.show()


解决方案

有关您的编辑code,包括在调用的 animation.FuncAnimation


  1. 帧= 25 25 选择为例) - 重新设置帧的最大数量

  2. 重复=假 - 帧的最大次数后,不要重复动画

相结合,你会得到这样的命令:

 动画= animation.FuncAnimation(图,更新,帧= 25,
                               间隔= 1000,位图传送= TRUE,重复= FALSE)

这导致它通过25帧(你的情况步骤一个动画,移动从 0 垂直线 24 在一秒的间隔) - 并停止在那里

I'm working on a script that moves a vertical line along an x-axis, over a series of datapoints. The animation works fine with plt.show but I am having trouble outputting a movie file. Please be aware that I am very new to Python, though I've been playing around with it for a year or two. The script was created by combining the first script in this tutorial with the script presented in the answer to this previous stack overflow question. The line will eventually be moving over a static data line chart, which I am presenting here as a diagonal line.

The movie file is output as having the correct length of time (1min, 10sec), but the line, which should move from the far left to the far right, 1-point per second, only moves a few pixels in the output video.

Any help you may be able to provide to solve this problem would be greatly appreciated.

Edit: I'm running Python 2.7.6 on Ubuntu 14.04.

Here is my reproducible code:

import numpy as np
from matplotlib import pyplot as plt
from matplotlib import animation
import time

# Simulated background data
x = np.linspace(0,61,62)
y = np.linspace(0,6,62)

# Set up the figure, the axis, and the plot element we want to animate
max_height = 6  # max height of y-axis
n_pts = 61      # max length of x-axis

# Original location for progress line
y1 = [0, max_height]
x1 = [0, 0]

fig = plt.figure()          # Initialize figure
#ax = fig.add_subplot(111)  # Intialize axes
ax = plt.axes(xlim=(0, n_pts), ylim=(0, max_height))    # Set axes limits
line, = ax.plot([], [], lw=2)                           # Initialize line

# draw the data to the 'background'
line1, = ax.plot(x, y, color='black')

# initialization function: plot the background of each frame
def init():
    line.set_data(x1, y1)
    return line,

starttime=time.time()
mytimer=0
mytimer_ref=0

# animation function.  This is called sequentially
def animate(i):
    t = time.time() - starttime
    mytimer = t + mytimer_ref
    x1 = [mytimer,mytimer]
    line.set_data(x1, y1)
    return line,

# call the animator.  
anim = animation.FuncAnimation(fig, animate, init_func=init,
                               frames=61, interval=1000)

# save the animation as an mp4.  This requires ffmpeg or mencoder to be
# installed.  The extra_args ensure that the x264 codec is used, so that
# the video can be embedded in html5.  You may need to adjust this for
# your system: for more information, see
# http://matplotlib.sourceforge.net/api/animation_api.html

writer = animation.writers['ffmpeg'](fps=1)

anim.save('demo.mp4',writer=writer,dpi=dpi)

plt.show()

Edit The following script creates the movie and saves it as an mp4. The problem now is that though there are 61 frames of animation, I can't get the movie to stop there. It proceeds to 100 frames every time. I know this question is a bit old now, but any help is greatly appreciated!

I have attempted to manually set the x-axis, which limits what is shown on the screen, but the animation continues beyond the shown axis nonetheless.

import numpy as np
from matplotlib import pyplot as plt
from matplotlib import animation
from time import time
from scipy.interpolate import interp1d

max_height = 6  # max height of y-axis
n_pts = 62      # max length of x-axis

x = np.linspace(0,61,62)
y = np.linspace(0,6,62)

# New figure with white background
fig = plt.figure(facecolor = 'white')
ax = plt.axes(aspect=1) # Set axes limits

# Original location for progress line
y1 = [0, max_height]
x1 = [0, 0]

# initialize line
plt.plot(x, y) #plot background
line, = ax.plot([], [], lw=2) 

def update(frame):
    x1 = frame
    line.set_data(x1, y1)

    # Return the modified line
    return line,

anim = animation.FuncAnimation(fig, update, interval=1000, blit=True)
anim.save('line.mp4', writer='ffmpeg')
plt.show() 

解决方案

For your edited code, include two additional arguments in the call to animation.FuncAnimation:

  1. frames=25 (25 chosen as an example) - set the max number of frames again
  2. repeat=False - do not repeat the animation after the max number of frames

Combined, you get this command:

anim = animation.FuncAnimation(fig, update, frames=25,
                               interval=1000, blit=True, repeat=False)

This results in a animation which steps through 25 frames (in your case, moves the vertical line from 0 to 24 in one second intervals) - and stops there.

这篇关于Matplotlib动画不保存正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆