我正在尝试运行一些Python 3代码,首先显示一个OpenCV窗口,当它关闭时应该使用MatplotLib绘制一些内容。但是,在plt.show()
调用时,程序崩溃了:python3.6意外退出。
import numpy as np
import matplotlib
matplotlib.use("TkAgg") # <== Doesn't solve the problem
import matplotlib.pyplot as plt
import cv2
#cv2.ocl.setUseOpenCL(False) <== Doesn't solve the problem
im = np.zeros((100,100,3))
cv2.imshow('test', im)
cv2.waitKey(0)
plt.plot(np.sin(np.linspace(0,20,200)))
plt.show() # <== Crashes on this call
在其他StackOverflow的帖子中,我发现将MatplotLib后端设置为
TkAgg
或设置setUseOpenCL(false)
应该可以解决这个问题。然而,在我的MacBook上,这两个潜在的解决方案都不起作用。我尝试了几件事情,比如改变导入顺序,尝试交换MatplotLib后端。有人知道如何解决这个问题吗?
MacOS Sierra 10.12.3Python 3.6.0
TKinter 版本8.5
OpenCV 版本3.2.0
MatplotLib 版本2.0.0
它崩溃时的堆栈跟踪: libc++abi.dylib: terminating with uncaught exception of type NSException 2017-03-28 11:30:45.639 python3.6[5208:78361] -[NSApplication _setup:]: unrecognized selector sent to instance 0x7f997ec98190 2017-03-28 11:30:45.641 python3.6[5208:78361] *因为向实例0x7f997ec98190发送了未识别的选择器“-[NSApplication _setup:]”,所以终止应用程序,抛出异常“NSInvalidArgumentException”* 第一次调用堆栈: ( 0 CoreFoundation 0x00007fffbeb80e7b 异常预处理+171 1 libobjc.A.dylib 0x00007fffd3760cad objc_exception_throw + 48 2 CoreFoundation 0x00007fffbec02cb4-[NSObject(NSObject) doesNotRecognizeSelector:]+132 3 CoreFoundation 0x00007fffbeaf2fb5*_CF_forwarding_* +1061 4 CoreFoundation 0x00007fffbeaf2b08_CF_forwarding_prep_0 +120 5 Tk 0x00000001032c45e6 TkpInit +471 6 Tk 0x000000010323fc8d Tk_Init +1794 7_tkinter.cpython-36m-darwin.so 0x000000010311cde6 Tcl_AppInit +82 8_tkinter.cpython-36m-darwin.so 0x00000001031183a3 _tkinter_create +1094 9 Python 0x0000000100b28ea2 _PyCFunction_FastCallDict +172 10 Python 0x0000000100b927a2 call_function +584 11 Python 0x0000000100b8fbbb _PyEval_EvalFrameDefault +23930 12 Python 0x0000000100b92fbf _PyEval_EvalCodeWithName +1973 13 Python 0x0000000100b938c4 _PyFunction_FastCallDict +477 14 Python 0x0000000100aeea73 _PyObject_FastCallDict +231 15 Python 0x0000000100aeeb93 _PyObject_Call_Prepend +149 16 Python 0x0000000100aee8cb PyObject_Call +102 17 Python 0x0000000100b3cd24 slot_tp_init +61 18 Python 0x0000000100b39ac3 type_call +184 19 Python 0x0000000100aeea34 _PyObject_FastCallDict +168 20 Python 0x0000000100b9263b call_function +225 21 Python 0x0000000100b8fbbb _PyEval_EvalFrameDefault +23930 22 Python 0x0000000100b93976 _PyFunction_FastCall +122 23 Python 0x0000000100b92775 call_function +539 24 Python 0x0000000100b8fbbb _PyEval_EvalFrameDefault +23930 25 Python 0x0000000100b92fbf _PyEval_EvalCodeWithName +1973 26 Python 0x0000000100b89e02 PyEval_EvalCodeEx +100 27 Python 0x0000000100b101e3 function_call +363 28 Python 0x0000000100aee8cb PyObject_Call +102 29 Python 0x0000000100b8fe16 _PyEval_EvalFrameDefault +24533 30 Python 0x0000000100b92fbf _PyEval_EvalCodeWithName +1973 31 Python 0x0000000100b936da fast_function +241 32 Python 0x0000000100b92775 call_function +539 33 Python 0x0000000100b8fbbb _PyEval_EvalFrameDefault +23930 34 Python 0x0000000100b93976 _PyFunction_FastCall +122 35 Python 0x0000000100b92775 call_function +539 36 Python 0x0000000100b8fbbb _PyEval_EvalFrameDefault +23930 37 Python 0x0000000100b92fbf _PyEval_EvalCodeWithName +1973 38 Python 0x0000000100b936da fast_function +241 39 Python 0x0000000100b92775 call_function +539 40 Python
matplotlib.use("TkAgg")
会生成相同的错误。 - Quang Hoang