Jupyter notebook 中的 Matplotlib 动画创建额外的空图 [英] Matplotlib animation in Jupyter notebook creates additional empty plot

查看:35
本文介绍了Jupyter notebook 中的 Matplotlib 动画创建额外的空图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经开始为 DSP 讲座创建一系列交互式笔记本.到目前为止,我已经设法复制 &实现下面粘贴的 MWE.然而,除了包含动画的 matplotlib 图,我总是得到一个空的 Matplotlib 窗口.任何想法如何抑制这种行为?

蟒蛇:3.6.3matplotlib:2.0 和 2.1IPython:5.3.0操作系统:Win 7 64 位

%matplotlib 内联将 numpy 导入为 np导入 matplotlib.pyplot 作为 plt从 matplotlib 导入动画从 IPython.display 导入 HTMLplt.rcParams['figure.figsize'] = (5,3)plt.rcParams['figure.dpi'] = 100plt.rcParams['savefig.dpi'] = 100plt.rcParams["animation.html"] = "jshtml" # 对于 matplotlib 2.1 及以上,使用 JavaScript#plt.rcParams["animation.html"] = "html5" # 对于 matplotlib 2.0 及以下版本,使用 ffmpeg 视频编解码器转换为 x264t = np.linspace(0,2*np.pi)x = np.sin(t)图, ax = plt.subplots()ax.axis([0,2*np.pi,-1,1])l, = ax.plot([],[])定义动画(i):l.set_data(t[:i], x[:i])ani = animation.FuncAnimation(fig, animate, frames=len(t))阿尼

也可以在以下位置查看笔记本:

I've started to create a series of interactive notebooks for a DSP lecture. So far, I've managed to copy & implement the MWE pasted below. However, in addition to the matplotlib figure containing the animation, I'm always getting an empty Matplotlib window. Any ideas how to suppress this behaviour?

python: 3.6.3 matplotlib: 2.0 and 2.1 IPython: 5.3.0 OS: Win 7 64 bit

%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

The notebook can also be viewed under:

https://github.com/chipmuenk/dsp_fpga/blob/master/notebooks/01_LTI/MWE_animation.ipynb

In the static rendering at github, only the empty plot window shows, not the JavaScript animation.

解决方案

This has nothing to do with an animation.

The lines

%matplotlib inline
import matplotlib.pyplot as plt
fig, ax = plt.subplots()

will create an output with an empty figure.

You may prevent the output of a cell in jupyter notebook using %%capture.

Cell1:

%%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))

Cell2:

ani

这篇关于Jupyter notebook 中的 Matplotlib 动画创建额外的空图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆