Table of Contents
问题现象
/anaconda3/envs/Python36/bin/python3.6 ……/MyMatplotlib.py
2019-03-19 22:56:47.073 python3.6[816:21632] -[NSApplication _setup:]: unrecognized selector sent to instance 0x7feafcc7f920
2019-03-19 22:56:47.075 python3.6[816:21632] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSApplication _setup:]: unrecognized selector sent to instance 0x7feafcc7f920'
*** First throw call stack:
(
0 CoreFoundation 0x00007fffcd4882cb __exceptionPreprocess + 171
1 libobjc.A.dylib 0x00007fffe229848d objc_exception_throw + 48
2 CoreFoundation 0x00007fffcd509f04 -[NSObject(NSObject) doesNotRecognizeSelector:] + 132
3 CoreFoundation 0x00007fffcd3fa755 ___forwarding___ + 1061
4 CoreFoundation 0x00007fffcd3fa2a8 _CF_forwarding_prep_0 + 120
5 libtk8.6.dylib 0x000000011652c31d TkpInit + 413
6 libtk8.6.dylib 0x000000011648417e Initialize + 2622
7 _tkinter.cpython-36m-darwin.so 0x0000000115b91a16 _tkinter_create + 1174
8 python3.6 0x0000000103074088 _PyCFunction_FastCallDict + 200
9 python3.6 0x000000010314af4f call_function + 143
10 python3.6 0x0000000103148abf _PyEval_EvalFrameDefault + 46847
11 python3.6 0x000000010313c209 _PyEval_EvalCodeWithName + 425
12 python3.6 0x000000010314bb1c _PyFunction_FastCallDict + 364
13 python3.6 0x0000000102ff28b0 _PyObject_FastCallDict + 320
14 python3.6 0x0000000103019fe8 method_call + 136
15 python3.6 0x0000000102ff9efe PyObject_Call + 62
16 python3.6 0x000000010309b385 slot_tp_init + 117
17 python3.6 0x000000010309f8c1 type_call + 241
18 python3.6 0x0000000102ff2821 _PyObject_FastCallDict + 177
19 python3.6 0x0000000102ffaa67 _PyObject_FastCallKeywords + 327
20 python3.6 0x000000010314b048 call_function + 392
21 python3.6 0x0000000103148b6f _PyEval_EvalFrameDefault + 47023
22 python3.6 0x000000010314b30c fast_function + 188
23 python3.6 0x000000010314afac call_function + 236
24 python3.6 0x0000000103148abf _PyEval_EvalFrameDefault + 46847
25 python3.6 0x000000010313c209 _PyEval_EvalCodeWithName + 425
26 python3.6 0x000000010314bb1c _PyFunction_FastCallDict + 364
27 python3.6 0x0000000102ff28b0 _PyObject_FastCallDict + 320
28 python3.6 0x0000000103019fe8 method_call + 136
29 python3.6 0x0000000102ff9efe PyObject_Call + 62
30 python3.6 0x0000000103148cc0 _PyEval_EvalFrameDefault + 47360
31 python3.6 0x000000010313c209 _PyEval_EvalCodeWithName + 425
32 python3.6 0x000000010314b3ba fast_function + 362
33 python3.6 0x000000010314afac call_function + 236
34 python3.6 0x0000000103148abf _PyEval_EvalFrameDefault + 46847
35 python3.6 0x000000010314b30c fast_function + 188
36 python3.6 0x000000010314afac call_function + 236
37 python3.6 0x0000000103148abf _PyEval_EvalFrameDefault + 46847
38 python3.6 0x000000010313c209 _PyEval_EvalCodeWithName + 425
39 python3.6 0x000000010314b3ba fast_function + 362
40 python3.6 0x000000010314afac call_function + 236
41 python3.6 0x0000000103148abf _PyEval_EvalFrameDefault + 46847
42 python3.6 0x000000010313c209 _PyEval_EvalCodeWithName + 425
43 python3.6 0x000000010314b3ba fast_function + 362
44 python3.6 0x000000010314afac call_function + 236
45 python3.6 0x0000000103148abf _PyEval_EvalFrameDefault + 46847
46 python3.6 0x000000010313c209 _PyEval_EvalCodeWithName + 425
47 python3.6 0x0000000103194d4c PyRun_FileExFlags + 252
48 python3.6 0x0000000103194224 PyRun_SimpleFileExFlags + 372
49 python3.6 0x00000001031bad66 Py_Main + 3734
50 python3.6 0x0000000102fea929 main + 313
51 libdyld.dylib 0x00007fffe2b7e235 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
Process finished with exit code 134 (interrupted by signal 6: SIGABRT)
解决办法
前提条件是假设你已经配置好project interpretater为“Python 3.6.7 (/anaconda3/envs/Python36/bin/python3.6)”(这个路径是我的Anaconda下虚拟环境Python36的Python解释器的路径)
在terminal终端下输入这个指令“vim ~/.matplotlib/matplotlibrc”(无双引号),
在打开的vim编辑器中先按字母i开启编辑模式,
然后输入“backend: TkAgg”(无双引号),如下图
接着按Esc键,
最后按“:wq”(无双引号)保存并退出
【个人喜欢用vim,各位朋友也可以用其他的编辑器在这个目录下~/.matplotlib/创建一个matplotlibrc文件,然后输入相同内容“backend: TkAgg”(无双引号)即可】
参考文章
https://stackoverflow.com/questions/21784641/installation-issue-with-matplotlib-python
Problem Cause In mac os image rendering back end of matplotlib (what-is-a-backend to render using the API of Cocoa by default). There is Qt4Agg and GTKAgg and as a back-end is not the default. Set the back end of macosx that is differ compare with other windows or linux os.
I resolve this issue following ways:
- I assume you have installed the pip matplotlib, there is a directory in you root called
~/.matplotlib
.- Create a file
~/.matplotlib/matplotlibrc
there and add the following code:backend: TkAgg
From this link you can try different diagram.
关于backend请参考
matplotlib.org/faq/usage_faq.html#what-is-a-backend
测试代码
#!usr/bin/env python
# -*- coding: UTF-8 -*-
import matplotlib.pyplot as plt
x = [1, 2, 4] # x轴数据
y = [4, 5, 6] # y轴数据
plt.plot(x, y) # 绘制图片
plt.show() # 显示图片