【问题标题】:Save animations created inside a for loop in python保存在python中的for循环内创建的动画
【发布时间】:2021-03-04 20:57:40
【问题描述】:

我有这个示例代码,它可以工作。我对 python 真的很陌生,我来自 Matlab 和 Octave。我需要在 FOR 循环中操作变量(以与给定示例不同且更复杂的方式),然后我需要 gif 文件中的动画输出。使用此代码,我可以使用所需的 fps 正确可视化动画,但我无法找到一种简单的方法来将 plt.show() 时看到的动画保存到 gif 文件或多个 png 文件中。我该怎么办?

# basic animated mod 39 wheel in python


import matplotlib.pyplot as plt
import numpy as np 
plt.close()
plt.rcParams.update({
    "lines.color": "white",
    "patch.edgecolor": "white",
    "text.color": "lightgray",
    "axes.facecolor": "black",
    "axes.edgecolor": "lightgray",
    "axes.labelcolor": "white",
    "xtick.color": "white",
    "ytick.color": "white",
    "grid.color": "lightgray",
    "figure.facecolor": "black",
    "figure.edgecolor": "black",
    "savefig.facecolor": "black",
    "savefig.edgecolor": "black"})
plt.xlabel('real axis')
plt.ylabel('imaginary axis')
plt.title('events constellation')
plt.xlim(-4, 4)
plt.ylim(-4, 4)
plt.gca().set_aspect('equal', adjustable='box')
plt.draw()
mng = plt.get_current_fig_manager()
mng.full_screen_toggle()

for n in range(1,40):

    cnums = 3 * np.exp(1j * 2 * np.pi * (1/39) * n)
    x = cnums.real 
    y = cnums.imag 
    plt.scatter(x, y , label="event", marker="o", color="red", s=200)
    plt.pause(1)

plt.show()


【问题讨论】:

  • 这可能对你有帮助:eli.thegreenplace.net/2016/…
  • 您好,感谢您的回复。在我询问之前我已经检查了该链接,但是我无法在我的代码中执行 plt.show() 之后或之前添加一个简单的命令,以将相同的动画保存在 gif、avi 或多个 png 文件中。这些选项中的任何一个都会有所帮助

标签: python animation


【解决方案1】:

如果您想导出图像,板上有很多问题的答案。 例如:https://stackoverflow.com/a/9890599/6102332

如果要导出为 gif 或 mp4 等。 同样,论坛上有很多很棒的回复。 例如:https://stackoverflow.com/a/25143651/6102332


特别是关于您的代码: 如果要保存多张图片,可以将这一行放在for循环的末尾:

for n in range(1, 10):
    cnums = 3 * np.exp(1j * 2 * np.pi * (1 / 39) * n)
    plt.scatter(cnums.real, cnums.imag, label="event", marker="o", color="red", s=200)
    plt.savefig(f'D:\\pic_{n}.png')

如果您想导出为 gif:您应该了解有关 ImageMagick、PillowWriter 或 imageio 的更多信息。 这里我以imageio为例如下:

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

fig, ax = plt.subplots()

ax.set(xlim=(-4, 4), ylim=(-4, 4))

red_dot = ax.scatter(None, None, color='red')


data = []


def plot_for_offset(n):
    cnums = 3 * np.exp(1j * 2 * np.pi * (1 / 39) * n)
    data.append([cnums.real, cnums.imag])
    red_dot.set_offsets(data)

    fig.canvas.draw()

    img = np.frombuffer(fig.canvas.tostring_rgb(), dtype='uint8')
    img = img.reshape(fig.canvas.get_width_height()[::-1] + (3,))

    return img


imageio.mimsave(r'F:\red-dot.gif', [plot_for_offset(n) for n in range(1, 40)], fps=1)

这是 gif 图像:

【讨论】:

  • 谢谢!! plt.savefig(/path) 是我所需要的,因为以后我可以自己创建动画或一帧一帧地使用!
【解决方案2】:

您可以使用 非常 简单的赛璐珞包从 matplotlib 动画中制作视频。 https://github.com/jwkvam/celluloid

小例子:

from matplotlib import pyplot as plt
from celluloid import Camera

fig = plt.figure()
camera = Camera(fig)
for i in range(10):
    plt.plot([i] * 10)
    camera.snap()
animation = camera.animate()
animation.save('output.gif')

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-28
    • 1970-01-01
    • 2015-08-10
    • 1970-01-01
    • 2016-10-17
    相关资源
    最近更新 更多