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

标签 matplotlib animation gif google-colaboratory animated-gif

我试图在谷歌的 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 )  

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

最佳答案

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

你需要设置它

from matplotlib import rc
rc('animation', html='jshtml')

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

这是一个 workable colab你的代码。

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

关于matplotlib - 在 Python 中嵌入 Matplotlib 动画(google colab notebook),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61103994/

相关文章:

matplotlib - 在 jupyter qtconsole 中自动运行 %matplotlib inline

python - 如何使用更新函数在 Matplotlib 2.0.0 中为 NetworkX 图形设置动画?

python - 更改 facecolor "alpha"值会产生不需要的边缘(matplotlib pcolormesh)

ios - 如何在 Swift 中缩放动画 GIF?

java - 小程序中gif动画闪烁

python - 如何从列表中绘制多个图例

css - SVG 动画不适用于 Google Chrome 或 IE

java - java中如何仅在动画完成后调用方法?

javascript - JS/jQuery - 悬停时仅播放一次 GIF

go - 在 go 中修改 gif 图像...未正确设置透明度