【发布时间】:2020-07-22 12:37:26
【问题描述】:
我正在编写将在 Google Colab 中显示的代码。 Colab 要求您在显示之前使用HTML 函数。这是一个示例代码(来自here):
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
from IPython.display import HTML
fig = plt.figure()
def f(x, y):
return np.sin(x) + np.cos(y)
x = np.linspace(0, 2 * np.pi, 120)
y = np.linspace(0, 2 * np.pi, 100).reshape(-1, 1)
im = plt.imshow(f(x, y), animated=True)
def updatefig(*args):
global x, y
x += np.pi / 15.
y += np.pi / 20.
im.set_array(f(x, y))
return im,
HTML(animation.FuncAnimation(fig, updatefig, interval=50, blit=True).to_jshtml())
这有效,除非您必须单击“播放”按钮才能播放。如下图:
是否可以生成 jshtml 以便在呈现框时立即播放,就好像它被人点击一样?请注意,代码需要在 Google Colab 中运行,根据我的经验,这比通常的 Jupyter notebook 的限制要多一些。
【问题讨论】:
标签: python matplotlib jupyter-notebook google-colaboratory