如何保存matplotlib动画的最后一帧图像?

4

我想知道如何在Python 3中保存FuncAnimation的最后一张图像。以下是一些代码:

import matplotlib.animation
from mpl_toolkits.mplot3d import Axes3D

ani = matplotlib.animation.FuncAnimation(fig, _update_2d_graph, frames=9, interval=600, repeat_delay=5000, blit=False)
ani.save(output_dir, writer="imagemagick")

其中 _update_2d_graph 是一个函数,根据帧编号提供不同的点偏移量:

def _update_2d_graph(num):
    x_updated = np.hstack((pos_gt.x[:num].tolist(),pos_pred.x[:num].tolist()))
    y_updated = np.hstack((pos_gt.y[:num].tolist(),pos_pred.y[:num].tolist()))

    paths_gt = []
    paths_ped = []

    for i in range(num):
        marker_gt = mmarkers.MarkerStyle('o')
        marker_ped = mmarkers.MarkerStyle('x')
        path_gt = marker_gt.get_path().transformed(marker_gt.get_transform())
        path_ped = marker_ped.get_path().transformed(marker_ped.get_transform())
        paths_gt.append(path_gt)
        paths_ped.append(path_ped)

    graph._paths = np.concatenate([paths_gt, paths_ped])
    graph.set_facecolors(np.concatenate([colors[:num], colors[:num]]))
    graph.set_edgecolors(np.concatenate([colors[:num], colors[:num]]))
    graph.set_offsets(np.hstack((y_updated[:, np.newaxis], x_updated[:, np.newaxis])))

与其保存整个动画(例如 .gif 文件),我想要保存最后一帧的 .png 文件。 是否可以访问动画对象中的最后一帧?

我不想调用 _update_2d_graph(9),因为这个调用已经被动画对象执行过了,也不想通过另一个 FuncAnimation 调用重新迭代整个 9 帧。


请提供 _update_2d_graph 的代码(或其简化形式),答案可能取决于您编写该函数的方式。它可以像运行 update_2d_graph(9) 一样简单,以获取最后一帧,或者您可能需要迭代所有帧以获得最终结果。 - Diziet Asahi
@DizietAsahi 完成了 - whiletrue
谢谢您指出这一点。我已经尝试澄清我的问题。 - whiletrue
FuncAnimation 不会存储每个单独的帧(这将非常消耗内存,但 ArtistAnimation 可以做到)。“我不想调用 _update_2d_graph(9)” 的原因是什么?(同时(9)还没有被调用,(8)将是最后一帧,对吗?) - ImportanceOfBeingErnest
我的错,是我在最后一帧写成了“9”,而不是“8”。 - Diziet Asahi
显示剩余2条评论
1个回答

3

实际上,我的初始测试表明,在运行动画后仅保存图形确实会保存最后一帧的状态。您可以测试以确认吗?

ani = matplotlib.animation.FuncAnimation(fig, _update_2d_graph, frames=9, interval=600, repeat_delay=5000, blit=False)
ani.save(output_dir, writer="imagemagick")
fig.savefig('last_frame.png')

我使用了3D随机漫步动画示例中的代码:

import numpy as np
import matplotlib.pyplot as plt
import mpl_toolkits.mplot3d.axes3d as p3
import matplotlib.animation as animation

# Fixing random state for reproducibility
np.random.seed(19680801)


def Gen_RandLine(length, dims=2):
    """
    Create a line using a random walk algorithm

    length is the number of points for the line.
    dims is the number of dimensions the line has.
    """
    lineData = np.empty((dims, length))
    lineData[:, 0] = np.random.rand(dims)
    for index in range(1, length):
        # scaling the random numbers by 0.1 so
        # movement is small compared to position.
        # subtraction by 0.5 is to change the range to [-0.5, 0.5]
        # to allow a line to move backwards.
        step = ((np.random.rand(dims) - 0.5) * 0.1)
        lineData[:, index] = lineData[:, index - 1] + step

    return lineData


def update_lines(num, dataLines, lines):
    for line, data in zip(lines, dataLines):
        # NOTE: there is no .set_data() for 3 dim data...
        line.set_data(data[0:2, :num])
        line.set_3d_properties(data[2, :num])
    return lines

# Attaching 3D axis to the figure
fig = plt.figure()
ax = p3.Axes3D(fig)

# Fifty lines of random 3-D lines
data = [Gen_RandLine(25, 3) for index in range(50)]

# Creating fifty line objects.
# NOTE: Can't pass empty arrays into 3d version of plot()
lines = [ax.plot(dat[0, 0:1], dat[1, 0:1], dat[2, 0:1])[0] for dat in data]

# Setting the axes properties
ax.set_xlim3d([0.0, 1.0])
ax.set_xlabel('X')

ax.set_ylim3d([0.0, 1.0])
ax.set_ylabel('Y')

ax.set_zlim3d([0.0, 1.0])
ax.set_zlabel('Z')

ax.set_title('3D Test')

# Creating the Animation object
line_ani = animation.FuncAnimation(fig, update_lines, 25, fargs=(data, lines),
                                   interval=50, blit=False, repeat=True)

### HERE ARE THE ONLY LINES I ADDED ####
line_ani.save('./test.gif', writer='imagemagick')
fig.savefig('./test.png') 

文件 test.gif

图片描述

文件 test.png

图片描述


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接