用Google Colab,怎样让matplotlib.animation图表动起来?

       今天在用colab做深度学习时,想要利用matplotlib.animation生成一个动图,但是一直无法使用,就在网上查找资料,看到了这篇教程,感觉很不错就转载过来了。

作者 Navjot
王小新 编译自 TowardsDataScience
量子位 出品 | 公众号 QbitAI

在这里插入图片描述
       Google Colab发布短短半年时间,受到了众多机器学习小伙伴的追捧。

       毕竟它无需安装、功能强大、支持实时协作,还能免费蹭Google云上的GPU,比Jupyter Notebooks不知高到哪里去了。
在这里插入图片描述
       可是,Colab也有不友好的地方:机器学习第一步,也就是用可视化图表分析数据的时候,你就可能遇到困难。想在Colab里绘制动图,比在Jupyter Notebooks里难多了。

       Jupyter Notebooks中,可以很容易地运行matplotlib库中的动图接口,但Colab中,就需要开动脑筋。

       为了在Google Colab中绘制动图,名叫Navjot的小哥在Medium上提出了一种方法。
在这里插入图片描述
       量子位搬运过来,以下为他博客的译文:

       让图表动起来,我见过的最佳方法是这样的 :

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

下面,我们会分步详细介绍。

任务:绘制y= sin(x)曲线,并得到每个点切线的动画。

首先,导入函数库:

import numpy as np
import matplotlib.pyplot as plt
from matplotlib import animation

定义自变量x的范围,并绘制sin(x)函数:

x = np.linspace(-4, 4, 100)
y = np.sin(x)

创建所需的绘图对象:

fig, ax = plt.subplots()
ax.set_xlim(( -4, 4))
ax.set_ylim((-2, 2))

再画出2条线,对应目标函数和它的切线:

line1, = ax.plot([], [], lw=2)
line2, = ax.plot([], [], lw=2)

接着,定义init函数,以设置动画中每一帧的背景:

def init():
    line1.set_data(x, y)      
    return (line1,)

然后,定义animate函数,并按顺序进行调用,得到每一帧中看出的变化:

def animate(i):
  at_x = x[i]
  # gradient_line will have the form m*x + b
  m = np.cos(at_x)
  b = np.sin(at_x) - np.cos(at_x)*at_x
  gradient_line = m*x + b

  line2.set_data(x, gradient_line)
  return (line2,)

最后,调用matplotlib.animation中FuncAnimation函数来设置动画参数:

anim = animation.FuncAnimation(fig, animate, init_func=init, frames=100, interval=100, blit=True)

       在该阶段,我们得到了动画对象anim,而且为了把动画内嵌到网页中,我们把图表中默认的表示设置为交互式的JavaScript小部件,把animate.html中的rc参数改为jshtml来实现。

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

       设置好上面内容后,我们就可以引用动画对象anim来显示内嵌动画图。

完整代码

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

# animate over some set of x, y
x = np.linspace(-4, 4, 100)
y = np.sin(x)

# First set up the figure, the axes, and the plot element
fig, ax = plt.subplots()
plt.close()
ax.set_xlim(( -4, 4))
ax.set_ylim((-2, 2))

line1, = ax.plot([], [], lw=2)
line2, = ax.plot([], [], lw=2)

# initialization function: plot the background of each frame
def init():
    line1.set_data(x, y)      
    return (line1,)

# animation function: this is called sequentially

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

# animate over some set of x, y
x = np.linspace(-4, 4, 100)
y = np.sin(x)

# First set up the figure, the axes, and the plot element
fig, ax = plt.subplots()
plt.close()
ax.set_xlim(( -4, 4))
ax.set_ylim((-2, 2))

line1, = ax.plot([], [], lw=2)
line2, = ax.plot([], [], lw=2)

# initialization function: plot the background of each frame
def init():
    line1.set_data(x, y)      
    return (line1,)

# animation function: this is called sequentially
def animate(i):
  at_x = x[i]

  # gradient_line will have the form m*x + b
  m = np.cos(at_x)
  b = np.sin(at_x) - np.cos(at_x)*at_x
  gradient_line = m*x + b

  line2.set_data(x, gradient_line)
  return (line2,)

anim = animation.FuncAnimation(fig, animate, init_func=init, frames=100, interval=100, blit=True)
rc('animation', html='jshtml')
anim

在Google Colab中运行这个代码,可生成下面的内嵌动画图。
在这里插入图片描述

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值