Mac PyCharm CE python matplotlib plt.show() 报错

在 PyCharm 中测试线性回归算法时,调用 plt.show() 时,
编译通过,运行报错。

2019-01-20 11:32:41.235 python[43029:5007789] -[NSApplication _setup:]: unrecognized selector sent to instance 0x7fe34e5ed2f0
2019-01-20 11:32:41.237 python[43029:5007789] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSApplication _setup:]: unrecognized selector sent to instance 0x7fe34e5ed2f0'
*** First throw call stack:
(
	0   CoreFoundation                      0x00007fff3226ce65 __exceptionPreprocess + 256
	1   libobjc.A.dylib                     0x00007fff5e2c7720 objc_exception_throw + 48
	2   CoreFoundation                      0x00007fff322ea22d -[NSObject(NSObject) __retain_OA] + 0
	3   CoreFoundation                      0x00007fff3220e820 ___forwarding___ + 1486
	4   CoreFoundation                      0x00007fff3220e1c8 _CF_forwarding_prep_0 + 120
	5   libtk8.6.dylib                      0x00000001156eb31d TkpInit + 413
	6   libtk8.6.dylib                      0x000000011564317e Initialize + 2622
	7   _tkinter.cpython-36m-darwin.so      0x000000011546ba16 _tkinter_create + 1174
	8   python                              0x0000000101253068 _PyCFunction_FastCallDict + 200
	9   python                              0x000000010132861f call_function + 143
	10  python                              0x0000000101326175 _PyEval_EvalFrameDefault + 46837
	11  python                              0x00000001013198c9 _PyEval_EvalCodeWithName + 425
	12  python                              0x00000001013292cc _PyFunction_FastCallDict + 364
	13  python                              0x00000001011d1f80 _PyObject_FastCallDict + 320
	14  python                              0x00000001011f95f8 method_call + 136
	15  python                              0x00000001011d95ce PyObject_Call + 62
	16  python                              0x000000010127a5b5 slot_tp_init + 117
	17  python                              0x000000010127eaf1 type_call + 241
	18  python                              0x00000001011d1ef1 _PyObject_FastCallDict + 177
	19  python                              0x00000001011da137 _PyObject_FastCallKeywords + 327
	20  python                              0x0000000101328718 call_function + 392
	21  python                              0x0000000101326225 _PyEval_EvalFrameDefault + 47013
	22  python                              0x00000001013289dc fast_function + 188
	23  python                              0x000000010132867c call_function + 236
	24  python                              0x0000000101326175 _PyEval_EvalFrameDefault + 46837
	25  python                              0x00000001013198c9 _PyEval_EvalCodeWithName + 425
	26  python                              0x00000001013292cc _PyFunction_FastCallDict + 364
	27  python                              0x00000001011d1f80 _PyObject_FastCallDict + 320
	28  python                              0x00000001011f95f8 method_call + 136
	29  python                              0x00000001011d95ce PyObject_Call + 62
	30  python                              0x0000000101326376 _PyEval_EvalFrameDefault + 47350
	31  python                              0x00000001013198c9 _PyEval_EvalCodeWithName + 425
	32  python                              0x0000000101328a8a fast_function + 362
	33  python                              0x000000010132867c call_function + 236
	34  python                              0x0000000101326175 _PyEval_EvalFrameDefault + 46837
	35  python                              0x00000001013289dc fast_function + 188
	36  python                              0x000000010132867c call_function + 236
	37  python                              0x0000000101326175 _PyEval_EvalFrameDefault + 46837
	38  python                              0x00000001013198c9 _PyEval_EvalCodeWithName + 425
	39  python                              0x0000000101328a8a fast_function + 362
	40  python                              0x000000010132867c call_function + 236
	41  python                              0x0000000101326175 _PyEval_EvalFrameDefault + 46837
	42  python                              0x00000001013198c9 _PyEval_EvalCodeWithName + 425
	43  python                              0x0000000101328a8a fast_function + 362
	44  python                              0x000000010132867c call_function + 236
	45  python                              0x0000000101326175 _PyEval_EvalFrameDefault + 46837
	46  python                              0x00000001013198c9 _PyEval_EvalCodeWithName + 425
	47  python                              0x000000010137255c PyRun_FileExFlags + 252
	48  python                              0x0000000101371a34 PyRun_SimpleFileExFlags + 372
	49  python                              0x00000001013987c6 Py_Main + 3734
	50  python                              0x00000001011c9f59 main + 313
	51  libdyld.dylib                       0x00007fff5f39608d start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException

原因: backend 设置不正确。
解决:

  1. 找到 matplotlib 配置文件 matplotlibrc
  2. 修改 backend

## non-interactive backend.
backend  : TkAgg
Mac PyCharm CE python matplotlib plt.show() 报错
修改后可正常调用 plt.show()