【问题标题】:Matplotlib animation not saving properlyMatplotlib 动画未正确保存
【发布时间】:2015-11-05 00:31:57
【问题描述】:

我正在编写一个脚本,该脚本在一系列数据点上沿 x 轴移动垂直线。 plt.show 的动画效果很好,但我无法输出电影文件。请注意,我对 Python 非常陌生,尽管我已经使用它一两年了。该脚本是通过将this tutorial 中的第一个脚本与this previous stack overflow question 的答案中提供的脚本相结合而创建的。这条线最终将在静态数据折线图上移动,我在这里将其显示为对角线。

电影文件被输出为具有正确的时间长度(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() 

【问题讨论】:

  • 仍然没有答案,但不知何故,保存动画时会忽略 interval 选项。奇怪的是,对animation.FuncAnimation 的调用实际上并没有创建动画。如果您包含一个保存每个 mytimer 值的变量,并打印此变量,您只会在调用 anim.save 之后才开始看到输出 - 但不是之前。然而,anim.save 只是调用你的动画 61 次,但没有遵守 interval
  • 感谢您的反馈。这给了我一些可以使用的东西,如果有任何事情我会更新。

标签: python animation matplotlib


【解决方案1】:

对于您编辑的代码,在对animation.FuncAnimation 的调用中包含两个附加参数:

  1. frames=25(以25为例)-再次设置最大帧数
  2. repeat=False - 在达到最大帧数后不再重复动画

结合起来,你得到这个命令:

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

这会导致动画经过 25 帧(在您的情况下,将垂直线从 0 以一秒的间隔移动到 24) - 并停在那里。

【讨论】:

  • 您好,感谢您的提醒!编辑:我想我投赞成票而不是接受。再次感谢。我最终对此进行了相当多的迭代,但这个答案对我解决问题非常重要。
猜你喜欢
  • 2013-08-03
  • 2014-12-08
  • 1970-01-01
  • 2020-07-12
  • 2014-07-14
  • 2017-03-26
  • 2018-12-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多