在 Python 中嵌入 Matplotlib 动画(google colab notebook)

use*_*933 3 animation matplotlib gif animated-gif google-colaboratory

我试图在谷歌的 colab.research 中显示一个 gif 文件。我能够使用以下路径名将文件保存在目录中,/content/BrowniamMotion.gif但我不知道如何在笔记本中显示此 GIF 以进行展示。

到目前为止生成 GIF 的代码,以防有人可以操纵它而不是保存 GIF 而是直接将其动画化到 google colab 文件中,

# Other Brownian Motion
from math import *
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits import mplot3d
import matplotlib.animation as animation

fig = plt.figure(figsize=(8,6))
ax = plt.axes(projection='3d')

N=10
#val1 = 500

x=500*np.random.random(N)
y=500*np.random.random(N)

z=500*np.random.random(N)

def frame(w):
    ax.clear()

    global x,y,z
    x=x+np.random.normal(loc=0.0,scale=50.0,size=10)
    y=y+np.random.normal(loc=0.0,scale=50.0,size=10)
    z=z+np.random.normal(loc=0.0,scale=50.0,size=10)


    plt.title("Brownian Motion")
    ax.set_xlabel('X(t)')
    ax.set_xlim3d(-500.0,500.0)
    ax.set_ylabel('Y(t)')
    ax.set_ylim3d(-500.0,500.0)
    ax.set_zlabel('Z(t)')


     ax.set_zlim3d(-500.0,500.0) 

        plot=ax.scatter

3D(x, y, z, c='r')


    return plot


anim = animation.FuncAnimation(fig, frame, frames=100, blit=False, repeat=True)

anim.save('BrowniamMotion.gif', writer = "pillow", fps=10 )  
Run Code Online (Sandbox Code Playgroud)

对不起,如果这个问题不好,说明。我是 Python 新手并使用 colab 研究。

Kor*_*ich 13

对于 Colab,最容易使用 'jshtml' 来显示 matplotlib 动画。

你需要设置它

from matplotlib import rc
rc('animation', html='jshtml')
Run Code Online (Sandbox Code Playgroud)

然后,只需键入您的动画对象。它会显示自己

anim
Run Code Online (Sandbox Code Playgroud)

这是您的代码的可行合作

它有一个滑块,您可以在任何时间点来回运行。