【发布时间】:2018-10-21 16:23:06
【问题描述】:
我想使用 Matplotlib 在 Jupyter 中显示动画。以下是一些基本示例:
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
fig, ax = plt.subplots()
line, = ax.plot(np.random.rand(10))
ax.set_ylim(0, 1)
def update(data):
line.set_ydata(data)
return line,
def data_gen():
while True:
yield np.random.rand(10)
ani = animation.FuncAnimation(fig, update, data_gen, interval=100);
from IPython.display import HTML
HTML(ani.to_jshtml())
但是,当我第二次运行相同的代码时,我在左下角得到一个剩余的:
我注意到当我在顶部添加%matplotlib inline 时,即使在重新启动内核后我也得到了错误的输出。因此我的猜测是,每次创建动画时,我都必须在顶部将魔术命令 %matplotlib 设置为默认值,但我什至找不到 %matplotlib 是否有默认值。
我使用 Anaconda。这是我的版本:
康达版本:4.4.10
Python版本:Python 3.6.4 :: Anaconda, Inc.
IPython 版本:6.2.1
Jupyter 版本:5.4.0
【问题讨论】:
-
我运行了你的代码,没有得到“剩余的”。
-
@10SecTom 第二次运行还可以吗?
-
当我打开一个新笔记本并且从未包含 %matplotlib inline 或 %matplotlib 时,我没有看到问题。当我添加 %matplotlib inline 时,我看到它发生了,即使取出 i%matplotlib inline 问题仍然存在。如果您打开一个新笔记本并运行代码,但从不添加 %matplotlib inline,您会遇到问题吗?
-
关于maplotlib majic的信息:ipython.readthedocs.io/en/stable/interactive/plotting.html
-
@10SecTom 不,但是当我第二次运行相同的代码或在新单元格中绘制第二个动画时,我得到了同样的错误。
标签: python animation matplotlib ipython jupyter-notebook