Python 在jupyter中抑制matplotlib动画中最后一帧的显示
我正在从事一个项目,该项目涉及使用帧的Python 在jupyter中抑制matplotlib动画中最后一帧的显示,python,jupyter-notebook,matplotlib-animation,Python,Jupyter Notebook,Matplotlib Animation,我正在从事一个项目,该项目涉及使用帧的pyplot.imshow生成matplotlib动画。我在一个jupyter笔记本上做这件事。我已经设法让它工作,但还有一个恼人的错误(或功能?)。创建动画后,Jupyter在输出单元格中显示动画的最后一帧。我希望输出包括动画,捕获为html,但不是这最后一帧。下面是一个简单的例子: import numpy as np from matplotlib import animation from IPython.display import HTML g
pyplot.imshow
生成matplotlib
动画。我在一个jupyter
笔记本上做这件事。我已经设法让它工作,但还有一个恼人的错误(或功能?)。创建动画后,Jupyter
在输出单元格中显示动画的最后一帧。我希望输出包括动画,捕获为html,但不是这最后一帧。下面是一个简单的例子:
import numpy as np
from matplotlib import animation
from IPython.display import HTML
grid = np.zeros((10,10),dtype=int)
fig1 = plt.figure(figsize=(8,8))
ax1 = fig1.add_subplot(1,1,1)
def animate(i):
grid[i,i]=1
ax1.imshow(grid)
return
ani = animation.FuncAnimation(fig1, animate,frames=10);
html = HTML(ani.to_jshtml())
display(html)
我可以使用捕获魔法
,但这会抑制一切。这是可以的,但我的最终目标是通过活页夹将其公开,并使其尽可能简单,便于学生使用
我在网上看到的matplotlib
动画似乎没有这个问题,但是那些使用plot而不是imshow
,这可能是个问题
如有任何建议,将不胜感激。
谢谢
大卫这是我在“朱皮特实验室”寻找的同一个东西的答案。只需添加
plt.close()
非常感谢!plt.close()正是我所需要的。
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import animation
from IPython.display import HTML
grid = np.zeros((10,10),dtype=int)
fig1 = plt.figure(figsize=(8,8))
ax1 = fig1.add_subplot(1,1,1)
def animate(i):
grid[i,i]=1
ax1.imshow(grid)
return
ani = animation.FuncAnimation(fig1, animate,frames=10);
html = HTML(ani.to_jshtml())
display(html)
plt.close() # update