【发布时间】:2018-04-18 16:18:45
【问题描述】:
我已经开始为 DSP 讲座创建一系列交互式笔记本。到目前为止,我已经成功复制并实现了下面粘贴的 MWE。然而,除了包含动画的 matplotlib 图,我总是得到一个空的 Matplotlib 窗口。任何想法如何抑制这种行为?
蟒蛇:3.6.3 matplotlib:2.0 和 2.1 IPython:5.3.0 操作系统:Win 7 64 位
%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
笔记本也可以在下面查看:
https://github.com/chipmuenk/dsp_fpga/blob/master/notebooks/01_LTI/MWE_animation.ipynb
在 github 的静态渲染中,只显示空的绘图窗口,不显示 JavaScript 动画。
【问题讨论】:
-
移除魔法
%matplotlib inline或改用%matplotlib agg。 -
我尝试了不同的后端,但问题是笔记本将在虚拟服务器上运行,学生将登录该虚拟服务器并与他们的浏览器进行交互。对我来说,将输出限制在一个窗口中似乎是最干净的解决方案。
-
两种解决方法都会在浏览器中呈现所有内容,但没有空图。
标签: python animation matplotlib plot jupyter