简体   繁体   中英

Error when running matplotlib in Anaconda

I am running the following code in an anaconda environment:

import matplotlib.pyplot as plt
plt.plot([1,2,3,4])
plt.ylabel('some numbers')
plt.show()

I am running the code in terminal on a mac, and I am getting the following error:

    2019-01-20 18:59:25.256 python3[16633:1195559] -[NSApplication _setup:]: unrecognized selector sent to instance 0x7ff7d49ab330
2019-01-20 18:59:25.259 python3[16633:1195559] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSApplication _setup:]: unrecognized selector sent to instance 0x7ff7d49ab330'
*** First throw call stack:
(
    0   CoreFoundation                      0x00007fff37c19ecd __exceptionPreprocess + 256
    1   libobjc.A.dylib                     0x00007fff63cd1720 objc_exception_throw + 48
    2   CoreFoundation                      0x00007fff37c97275 -[NSObject(NSObject) __retain_OA] + 0
    3   CoreFoundation                      0x00007fff37bbbb40 ___forwarding___ + 1486
    4   CoreFoundation                      0x00007fff37bbb4e8 _CF_forwarding_prep_0 + 120
    5   libtk8.6.dylib                      0x00000001173f731d TkpInit + 413
    6   libtk8.6.dylib                      0x000000011734f17e Initialize + 2622
    7   _tkinter.cpython-36m-darwin.so      0x0000000117179a46 _tkinter_create + 1174
    8   python3                             0x000000010b01f958 _PyCFunction_FastCallDict + 200
    9   python3                             0x000000010b0f6b5f call_function + 143
    10  python3                             0x000000010b0f4881 _PyEval_EvalFrameDefault + 47217
    11  python3                             0x000000010b0e7e49 _PyEval_EvalCodeWithName + 425
    12  python3                             0x000000010b0f772c _PyFunction_FastCallDict + 364
    13  python3                             0x000000010af9e530 _PyObject_FastCallDict + 320
    14  python3                             0x000000010afc59a8 method_call + 136
    15  python3                             0x000000010afa5b8e PyObject_Call + 62
    16  python3                             0x000000010b046d55 slot_tp_init + 117
    17  python3                             0x000000010b04b2a1 type_call + 241
    18  python3                             0x000000010af9e4a1 _PyObject_FastCallDict + 177
    19  python3                             0x000000010afa66f7 _PyObject_FastCallKeywords + 327
    20  python3                             0x000000010b0f6c58 call_function + 392
    21  python3                             0x000000010b0f4931 _PyEval_EvalFrameDefault + 47393
    22  python3                             0x000000010b0f6f1c fast_function + 188
    23  python3                             0x000000010b0f6bbc call_function + 236
    24  python3                             0x000000010b0f4881 _PyEval_EvalFrameDefault + 47217
    25  python3                             0x000000010b0e7e49 _PyEval_EvalCodeWithName + 425
    26  python3                             0x000000010b0f772c _PyFunction_FastCallDict + 364
    27  python3                             0x000000010af9e530 _PyObject_FastCallDict + 320
    28  python3                             0x000000010afc59a8 method_call + 136
    29  python3                             0x000000010afa5b8e PyObject_Call + 62
    30  python3                             0x000000010b0f4a77 _PyEval_EvalFrameDefault + 47719
    31  python3                             0x000000010b0e7e49 _PyEval_EvalCodeWithName + 425
    32  python3                             0x000000010b0f6fca fast_function + 362
    33  python3                             0x000000010b0f6bbc call_function + 236
    34  python3                             0x000000010b0f4881 _PyEval_EvalFrameDefault + 47217
    35  python3                             0x000000010b0f6f1c fast_function + 188
    36  python3                             0x000000010b0f6bbc call_function + 236
    37  python3                             0x000000010b0f4881 _PyEval_EvalFrameDefault + 47217
    38  python3                             0x000000010b0e7e49 _PyEval_EvalCodeWithName + 425
    39  python3                             0x000000010b0f6fca fast_function + 362
    40  python3                             0x000000010b0f6bbc call_function + 236
    41  python3                             0x000000010b0f4881 _PyEval_EvalFrameDefault + 47217
    42  python3                             0x000000010b0e7e49 _PyEval_EvalCodeWithName + 425
    43  python3                             0x000000010b0f6fca fast_function + 362
    44  python3                             0x000000010b0f6bbc call_function + 236
    45  python3                             0x000000010b0f4881 _PyEval_EvalFrameDefault + 47217
    46  python3                             0x000000010b0e7e49 _PyEval_EvalCodeWithName + 425
    47  python3                             0x000000010b14040c PyRun_FileExFlags + 252
    48  python3                             0x000000010b13fb6e PyRun_SimpleFileExFlags + 366
    49  python3                             0x000000010b1665b6 Py_Main + 3766
    50  python3                             0x000000010af965c9 main + 313
    51  libdyld.dylib                       0x00007fff64d9fed9 start + 1
    52  ???                                 0x0000000000000002 0x0 + 2
)
libc++abi.dylib: terminating with uncaught exception of type NSException
Abort trap: 6

For some reason, I don't get the error when I am not in an Anaconda, but I would like to use the environment because other parts of my programs require several other packages with dependencies. How can I fix this error?

The quick answer is: conda install python.app and use the pythonw command instead of python

In macOS, two different types of Python builds exist: a regular build and a framework build. In order to interact correctly with macOS through the native GUI frameworks, you need a framework build of Python. ( https://matplotlib.org/faq/osx_framework.html )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM