在Python中嵌入Matplotlib动画(Google Colab笔记本) [英] Embedding Matplotlib Animations in Python (google colab notebook)

查看:234
本文介绍了在Python中嵌入Matplotlib动画(Google Colab笔记本)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Google的colab.research中显示gif文件.我能够使用以下路径名/content/BrowniamMotion.gif将文件保存在目录中,但是我不知道如何在笔记本中显示此GIF图像.

I am trying to show a gif file in google's colab.research. I was able to save the file in the directory with the following path name /content/BrowniamMotion.gif but I don't know how to show this GIF in my notebook to present.

到目前为止,如果有人可以操纵它而不是保存GIF而是直接将其设置为google colab文件的动画,那么生成GIF的代码是

The code to generate the GIF so far, in case someone can manipulate it not to save the GIF but rather to animate it directly into the google colab file was,

# 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研究.

Sorry if this question is badly, stated. I am new to Python and using colab research.

推荐答案

对于Colab,最简单的方法是使用"jshtml"显示matplotlib动画.

For Colab it is easiest to use 'jshtml' to display matplotlib animation.

您需要使用

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

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

Then, just type your animation object. It will display itself

anim

这是您的代码的可行的合作.

它有一个滑块,您可以在任何时间来回奔跑.

It has a slider where you can run back and forth at any point in time.

这篇关于在Python中嵌入Matplotlib动画(Google Colab笔记本)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆