【问题标题】:Is there a way in Colab to generate animation dynamically?Colab 有没有办法动态生成动画?
【发布时间】:2019-03-08 03:41:44
【问题描述】:

我编写了一个 Langton 的 ant 代码,我想让动画在 Colab 中运行,直到它被用户停止或在一定数量的帧之后。就像现在一样,它首先生成所有帧,然后将它们编译成动画,然后显示出来。如果有很多帧,则需要很长时间和/或 Colab 内存不足。这就是为什么我希望有一种方法可以一次生成一帧并不断更新图像。 FuncAnimation 似乎没有这种能力,但也许我只是没有看到它。 如果有人知道有用的方法或文档,请告诉我。

import numpy as np
import matplotlib.pyplot as plt
from matplotlib import animation, rc, colors
from IPython.display import HTML

N = 40
ant = np.array([N//2, N//2])
move = {'N': [0, 1], 'E': [1, 0], 'S': [0, -1], 'W': [-1, 0]}
d = ['N', 'E', 'S', 'W']
facing = 1
board = np.zeros((N, N))
color = 0
board[ant[0]][ant[1]] = 4

cmap = colors.ListedColormap(['darkgreen', 'limegreen', 'greenyellow', 'yellow', 'red'])


def turn(direction):
    if direction == 'R':
        return (facing + 1) % 4
    else:
        return (facing - 1) % 4


def update(data):
    global ant, board, facing, color
    if color in [0, 1]:
        facing = turn('R')
    else:
        facing = turn('L')

    board[ant[0]][ant[1]] = (color + 1) % 4
    ant += move[str(d[facing])]
    color = board[ant[0]][ant[1]]
    board[ant[0]][ant[1]] = 4

    mat.set_data(board)
    return [mat]

fig, ax = plt.subplots(figsize=(5, 5));

ax.grid(False)
plt.axis('off')

mat = ax.matshow(board, cmap=cmap)
ani = animation.FuncAnimation(fig, update, frames = 150, interval = 1, repeat=False, blit=True)

rc('animation', html='jshtml')
ani

【问题讨论】:

    标签: python animation google-colaboratory


    【解决方案1】:

    您的代码似乎缺少更新。根据您的描述,您正在制作大量情节而没有清除它们。看看这个奇怪的 gif 东西的一些标志

    from IPython.display import SVG, display
    out=display(SVG(url='http://upload.wikimedia.org/wikipedia/en/a/a4/Flag_of_the_United_States.svg'), display_id=True)
    while True:
        out.update(SVG(url='https://upload.wikimedia.org/wikipedia/commons/f/fa/Flag_of_the_People%27s_Republic_of_China.svg'))
        out.update(SVG(url='http://upload.wikimedia.org/wikipedia/en/a/a4/Flag_of_the_United_States.svg'))
    

    这里的关键部分是 out.update。你有一个更新功能。

    空格

    看起来 Goolge Colab 中也存在小部件,并且他们有一个笔记本可以显示:https://colab.research.google.com/notebooks/widgets.ipynb

    而且看起来你可以使用 Bokeh 制作动画,我不知道这是否可能,但我 100% 确定这是可能的。 https://colab.research.google.com/notebooks/charts.ipynb#scrollTo=nv8P3UYm6SiQ

    【讨论】:

      【解决方案2】:

      您可以在 FuncAnimation 参数中添加帧并使用以下命令。 虽然有一个限制,但这会增加你动画中的一些帧。这只是将动画限制提高到一个较大的值。

      matplotlib.rcParams['animation.embed_limit'] = 2**128
      

      【讨论】:

        【解决方案3】:

        啊,这花了我四个小时,但完全值得!

        out = display(Image.fromarray((cmap(board)*255.0).astype('uint8'),'RGBA'), display_id=True)
        for i in range(200):
            #print(cmap(board))
            time.sleep(0.1)
            update(board)
            image=Image.fromarray(cmap(board),'RGBA')
            out.update(Image.fromarray((cmap(board)*255).astype('uint8'),'RGBA'))
        

        【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-10-01
        • 1970-01-01
        • 1970-01-01
        • 2012-03-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多