2015-10-17 45 views
1

我正在处理一个脚本,该脚本沿x轴移动一系列数据点的垂直线。该动画与plt.show正常工作,但我无法输出电影文件。请注意,我对Python很新,尽管我一直在玩它一两年。该脚本是通过将this tutorial中的第一个脚本与this previous stack overflow question的答案中提供的脚本相结合而创建的。这条线最终将在静态数据线图上移动,我在这里将其作为对角线呈现。Matplotlib动画未正确保存

电影文件被输出为具有正确的时间长度(1分10秒),但是应该从最左边移动到最右边每秒1点的线仅移动几个像素输出视频。

任何帮助,您可能能够提供解决这个问题将不胜感激。

编辑:我在Ubuntu 14.04上运行Python 2.7.6。

这里是我的重复性代码:

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() 

编辑下面的脚本创建的电影,并将其保存为MP4。现在的问题是,虽然有61帧动画,但我无法让电影在那里停下来。它每次都会进入100帧。我知道这个问题现在有点老,但任何帮助非常感谢!

我试图手动设置x轴,它限制了屏幕上显示的内容,但动画仍然继续超出显示的轴。

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() 
+1

仍然没有答案,但不知何故,保存动画时忽略'interval'选项。奇怪的是,对“animation.FuncAnimation”的调用实际上并不创建动画。如果您包含一个保存每个mytimer值的变量,并打印此变量,则只有在调用'anim.save'后才能看到输出 - 但之前不会。然而,'anim.save'只是调用你的动画61次,但不遵守'interval'。 – Schorsch

+0

感谢您的反馈。这给了我一点工作,我会更新,如果涉及到任何事情。 –

回答

1

对于已编辑的代码,包括在呼叫两个附加参数animation.FuncAnimation

  1. frames=2525选择作为一个例子) - 设定的帧的最大数量再次
  2. repeat=False - DO在最大帧数后不重复动画

合并后,您会得到以下命令:

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

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

+0

谢谢。这工作! –

+0

嗨,谢谢你的提醒!编辑:我认为我打了upvote而不是接受。再次感谢。我最终对此进行了很多迭代,但是这个答案对于我的问题解决来说非常重要。 –