【问题标题】:how can I keep the last frame when I use matplotlib animation function with blit = True当我使用带有 blit = True 的 matplotlib 动画函数时,如何保留最后一帧
【发布时间】:2019-03-08 00:36:29
【问题描述】:

我必须设置 blit = true,因为绘图要快得多。但是在动画之后(repeat = false),如果我使用放大图,图就会消失。我需要保留最后一帧,以便放大最后一个图。

谢谢!

【问题讨论】:

  • 我目前不确定原因,但我提交了an issue,以免被遗忘。
  • 谢谢!我正在考虑一些技巧来“解决”这个问题,比如在动画结束后再次仅绘制最后一帧。
  • 我遇到了同样的问题,作为一种解决方法,我修改了传递给 FuncAnimation 的帧生成器,以保持无限生成最后一个元素。

标签: animation matplotlib blit


【解决方案1】:

一种解决方法是使用最后一帧初始化动画。明显的缺点是您必须预先计算最后一帧。适应this example 将是

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation

fig, ax = plt.subplots()

x = np.arange(0, 2*np.pi, 0.01)
line, = ax.plot(x, np.sin(x))

cnt = 50 # Define so we know what the last frame will be.

def init():
    # Note the function is the same as `animate` with `i` set to the last value
    line.set_ydata(np.sin(x + cnt / 100))
    return line,

def animate(i):
    line.set_ydata(np.sin(x + i / 100))  # update the data.
    return line,

ani = animation.FuncAnimation(
    fig, animate, init_func=init, interval=2, blit=True, save_count=cnt)

ani.save("mwe.mov")
fig.savefig("mwe.png")

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-31
    • 2021-05-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多