Matplotlib动画可以抑制jupyter笔记本中的错误消息

时间:2017-11-04 10:56:30

标签: python matplotlib jupyter-notebook

我想调试使用matplotlib' s FuncAnimation的代码来为jupyter笔记本中的绘图设置动画。

但是,我注意到更新函数中没有传递错误消息。例如,下面的代码显式引发了错误,但其消息从未显示在输出中。运行只是停止。

我的问题是,如何重定向错误流以便调试我的(更复杂的)代码?

%matplotlib nbagg

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation


def get_image():
    return np.random.random((20, 20))


def updatefig(*args):
    """Update and return image."""

    raise NameError() # <-- Error does not show!

    im.set_array(get_image())
    return im,


fig = plt.figure()
im = plt.imshow(get_image(), animated=True)

ani = FuncAnimation(fig, updatefig, interval=20.0, blit=True)
plt.show()

0 个答案:

没有答案