首页 > 解决方案 > Matplotlib 在 pycharm 中导致运行时错误

问题描述

当我在 pycharm IDE 中绘制多个图形时,matplotlib 出现问题:

import matplotlib
matplotlib.use('TkAgg')
from matplotlib import pyplot as plt
num = 1 
fig = plt.figure()
ax1 = fig.add_subplot(2, 2, 1)#axarr[0, 0]
im1 = ax1.imshow(test[num][0], vmin=15,  cmap='gray', aspect='equal')
ax1.set_title("test title 1")
fig.colorbar(im1, ax=ax1)
ax2 = fig.add_subplot(2, 2, 2)
im2 = ax2.imshow(test2[num][0], vmin=15,  cmap='gray', aspect='equal')
ax2.set_title("test title2")
fig.colorbar(im2, ax=ax2)
ax3 = fig.add_subplot(2, 2, 3)
im3 = ax3.imshow(test3[num][0], vmin=15,  cmap='gray',aspect='equal')
ax3.set_title("test title3")
fig.colorbar(im3, ax=ax3)
ax4 = fig.add_subplot(2, 2, 4)
im4 = ax4.imshow(test4[num][0], vmin=1300, vmax=1700, aspect='equal')
ax4.set_title("test tiltle 4")
fig.colorbar(im4, ax=ax4)

test1-4 数组是ndarray:(700,1,256,256)

在上面添加的单次或多次运行代码后随机出现以下错误:

Exception ignored in: <bound method Image.__del__ of <tkinter.PhotoImage object at 0x7f1482693518>>
Traceback (most recent call last):
File "/usr/lib/python3.6/tkinter/__init__.py", line 3507, in __del__
     self.tk.call('image', 'delete', self.name)
RuntimeError: main thread is not in main loop
Tcl_AsyncDelete: async handler deleted by the wrong thread

和 pycharm 控制台返回以下并冻结:

 Process finished with exit code 134 (interrupted by signal 6: SIGABRT)

建议使用一些帖子,matplotlib.use('Agg')但我希望显示情节。我用matplotlib 3.0.3

为什么此错误随机发生,我该如何解决?

标签: pythonmatplotlibpycharmruntime-error

解决方案


推荐阅读