Python Jupyter笔记本中的Matplotlib动画创建额外的空打印
我已经开始为DSP讲座创建一系列交互式笔记本。到目前为止,我已经成功地复制并实现了粘贴在下面的MWE。然而,除了包含动画的matplotlib图形之外,我总是得到一个空的matplotlib窗口。有没有办法抑制这种行为 python:3.6.3 matplotlib:2.0和2.1 伊皮顿:5.3.0 操作系统:Win7 64位Python Jupyter笔记本中的Matplotlib动画创建额外的空打印,python,animation,matplotlib,plot,jupyter,Python,Animation,Matplotlib,Plot,Jupyter,我已经开始为DSP讲座创建一系列交互式笔记本。到目前为止,我已经成功地复制并实现了粘贴在下面的MWE。然而,除了包含动画的matplotlib图形之外,我总是得到一个空的matplotlib窗口。有没有办法抑制这种行为 python:3.6.3 matplotlib:2.0和2.1 伊皮顿:5.3.0 操作系统:Win7 64位 %matplotlib inline import numpy as np import matplotlib.pyplot as plt from matplotl
%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import animation
from IPython.display import HTML
plt.rcParams['figure.figsize'] = (5,3)
plt.rcParams['figure.dpi'] = 100
plt.rcParams['savefig.dpi'] = 100
plt.rcParams["animation.html"] = "jshtml" # for matplotlib 2.1 and above, uses JavaScript
#plt.rcParams["animation.html"] = "html5" # for matplotlib 2.0 and below, converts to x264 using ffmpeg video codec
t = np.linspace(0,2*np.pi)
x = np.sin(t)
fig, ax = plt.subplots()
ax.axis([0,2*np.pi,-1,1])
l, = ax.plot([],[])
def animate(i):
l.set_data(t[:i], x[:i])
ani = animation.FuncAnimation(fig, animate, frames=len(t))
ani
还可以在以下位置查看笔记本:
在github的静态渲染中,只显示空的plot窗口,而不显示JavaScript动画。这与动画无关 台词
%matplotlib inline
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
将创建一个带有空图形的输出
您可以使用%%capture
阻止jupyter笔记本中的单元格输出
第1单元:
%%capture
%matplotlib inline
import matplotlib.pyplot as plt
import matplotlib.animation
plt.rcParams["animation.html"] = "jshtml"
import numpy as np
t = np.linspace(0,2*np.pi)
x = np.sin(t)
fig, ax = plt.subplots()
h = ax.axis([0,2*np.pi,-1,1])
l, = ax.plot([],[])
def animate(i):
l.set_data(t[:i], x[:i])
ani = matplotlib.animation.FuncAnimation(fig, animate, frames=len(t))
第2单元:
ani
以下是另一个示例:
%matplotlib inline
from matplotlib import animation, pyplot as plt
import numpy as np
plt.rc('animation', html='html5')
data = np.random.random(20)
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(data) # draw background
anim = animation.ArtistAnimation(fig, [[ax.scatter(x, y)] for x, y in enumerate(data)])
anim
结果(anim
)显示为动画,但潜在的副作用是额外显示静态帧。如果plt.figure
调用发生在add\u子批
方法之前的单独单元格中,则此副作用消失
这是因为(正如ImportanceOfBeingErnest所说)创建一个新图形会产生显示静态图像的副作用(描述在笔记本中当前单元格评估结束时图形是如何留下的)。但是,如果图形上还没有填充任何内容(甚至轴也没有),则会阻止显示任何图像(无需jupyter magic来抑制它)。您可以在最后一行之前添加
plt.close()
%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import animation
from IPython.display import HTML
plt.rcParams['figure.figsize'] = (5,3)
plt.rcParams['figure.dpi'] = 100
plt.rcParams['savefig.dpi'] = 100
plt.rcParams["animation.html"] = "jshtml" # for matplotlib 2.1 and above, uses JavaScript
#plt.rcParams["animation.html"] = "html5" # for matplotlib 2.0 and below, converts to x264 using ffmpeg video codec
t = np.linspace(0,2*np.pi)
x = np.sin(t)
fig, ax = plt.subplots()
ax.axis([0,2*np.pi,-1,1])
l, = ax.plot([],[])
def animate(i):
l.set_data(t[:i], x[:i])
ani = animation.FuncAnimation(fig, animate, frames=len(t))
plt.close()
ani
删除magic
%matplotlib inline
或改用%matplotlib agg
。我尝试了不同的后端,但问题是笔记本将在虚拟服务器上运行,学生将登录到该服务器并与浏览器交互。对我来说,将输出限制在一个窗口似乎是最干净的解决方案。两种解决方案都在浏览器中呈现所有内容,但没有空的图形。整洁!在浏览%%capture时,我发现了另一个解决方案:fig,ax=plt.subplot()plt.close();也为我做了这个把戏。关闭绘图时,fig
和ax
的句柄显然不会被删除。是否有理由选择其中一种或另一种解决方案?不,两种解决方案工作的原因实际上是相同的:绘图是闭合的。可以移动到下一个单元格,也可以关闭单元格中已存在的单元格。选择你更喜欢的方法。只需控制按钮!当我将其放入函数中时,这仅绘制x值。我发现在创建动画的行(ani=…
)之后使用plt.close()
)对于防止动画为空非常重要。另一种选择是完全忽略%matplotlib inline
,或者如果这不可行,然后,在创建用于动画的图形后立即重复该操作。这重置了显示静态图形的魔力,而静态图形不是单元格结果的一部分;动画本身是通过一种完全不同的机制来显示的(包括单元格计算结果的html格式)!谢谢