matplotlib 第二次执行报错在 django web服务中

报错内容如下:

Exception RuntimeError: RuntimeError('main thread is not in main loop',) in <bound method PhotoImage.del of < Tkinter.PhotoImage instance at 0x7f347192b5a8>> ignored

解决方法:最代码最顶层加:

import matplotlib
matplotlib.use('Agg')

要在 pylab 和 matplotlib.pyplot 之前,否则无效。
这里有一个容易让人忽视的地方,因为有些python 库 也包含了pylab 或 matplotlib.pyplot, 如果上述代码未加在他们之前,则会无效,不熟悉的话很难找到原因,所以最保险的是加代码文件的在最顶层。

posted @ 2017-09-14 18:25  微微苏荷  阅读(719)  评论(0编辑  收藏  举报