3
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Matplotlibを使ってグラフの描画ができない!

Last updated at Posted at 2019-04-14

初投稿です.メモ代わりに投稿していこうかなと思ってます.
本記事はmatplotlibのライブラリのインストールでつまづいた時に役立つかもしれません.

前提

  • python==3.6
  • anaconda
  • matplotlib==3.0.3

やろうとしたこと

Anacondaで環境を構築して,matplotlibをインストール.

conda install matplotlib

なんの変哲も無いコサインカーブを表示するプログラムを実行した時に問題が発生しました.

import matplotlib.pyplot as plt
import numpy as np

x = np.arange(10)
y = np.cos(x)
plt.plot(x, y)
plt.show()

エラー

2019-04-14 15:45:06.582 python3[59308:5238302] -[NSApplication _setup:]: unrecognized selector sent to instance 0x7fc920797ea0
2019-04-14 15:45:06.585 python3[59308:5238302] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', \
reason: '-[NSApplication _setup:]: unrecognized selector sent to instance 0x7fc920797ea0'
*** First throw call stack:
(
        0   CoreFoundation                      0x00007fff5057c68b __exceptionPreprocess + 171
        1   libobjc.A.dylib                     0x00007fff7780dc76 objc_exception_throw + 48
        2   CoreFoundation                      0x00007fff506151e4 -[NSObject(NSObject) doesNotRecognizeSelector:] + 132
        3   CoreFoundation                      0x00007fff504f2b50 ___forwarding___ + 1456
        4   CoreFoundation                      0x00007fff504f2518 _CF_forwarding_prep_0 + 120
        5   libtk8.6.dylib                      0x00000001127d731d TkpInit + 413
        6   libtk8.6.dylib                      0x000000011272f17e Initialize + 2622
        7   _tkinter.cpython-36m-darwin.so      0x000000010f9f1a16 _tkinter_create + 1174
        8   python3                             0x000000010cc08068 _PyCFunction_FastCallDict + 200
        9   python3                             0x000000010ccdd61f call_function + 143
        10  python3                             0x000000010ccdb175 _PyEval_EvalFrameDefault + 46837
        11  python3                             0x000000010ccce8c9 _PyEval_EvalCodeWithName + 425
        12  python3                             0x000000010ccde2cc _PyFunction_FastCallDict + 364
        13  python3                             0x000000010cb86f80 _PyObject_FastCallDict + 320
        14  python3                             0x000000010cbae5f8 method_call + 136
        15  python3                             0x000000010cb8e5ce PyObject_Call + 62
        16  python3                             0x000000010cc2f5b5 slot_tp_init + 117
        17  python3                             0x000000010cc33af1 type_call + 241
        18  python3                             0x000000010cb86ef1 _PyObject_FastCallDict + 177
        19  python3                             0x000000010cb8f137 _PyObject_FastCallKeywords + 327
        20  python3                             0x000000010ccdd718 call_function + 392
        21  python3                             0x000000010ccdb225 _PyEval_EvalFrameDefault + 47013
        22  python3                             0x000000010ccdd9dc fast_function + 188
        23  python3                             0x000000010ccdd67c call_function + 236
        24  python3                             0x000000010ccdb175 _PyEval_EvalFrameDefault + 46837
        25  python3                             0x000000010ccce8c9 _PyEval_EvalCodeWithName + 425
        26  python3                             0x000000010ccde2cc _PyFunction_FastCallDict + 364
        27  python3                             0x000000010cb86f80 _PyObject_FastCallDict + 320
        28  python3                             0x000000010cbae5f8 method_call + 136
        29  python3                             0x000000010cb8e5ce PyObject_Call + 62
        30  python3                             0x000000010ccdb376 _PyEval_EvalFrameDefault + 47350
        31  python3                             0x000000010ccce8c9 _PyEval_EvalCodeWithName + 425
        32  python3                             0x000000010ccdda8a fast_function + 362
        33  python3                             0x000000010ccdd67c call_function + 236
        34  python3                             0x000000010ccdb175 _PyEval_EvalFrameDefault + 46837
        35  python3                             0x000000010ccdd9dc fast_function + 188
        36  python3                             0x000000010ccdd67c call_function + 236
        37  python3                             0x000000010ccdb175 _PyEval_EvalFrameDefault + 46837
        38  python3                             0x000000010ccce8c9 _PyEval_EvalCodeWithName + 425
        39  python3                             0x000000010ccdda8a fast_function + 362
        40  python3                             0x000000010ccdd67c call_function + 236
        41  python3                             0x000000010ccdb175 _PyEval_EvalFrameDefault + 46837
        42  python3                             0x000000010ccce8c9 _PyEval_EvalCodeWithName + 425
        43  python3                             0x000000010ccdda8a fast_function + 362
        44  python3                             0x000000010ccdd67c call_function + 236
        45  python3                             0x000000010ccdb175 _PyEval_EvalFrameDefault + 46837
        46  python3                             0x000000010ccce8c9 _PyEval_EvalCodeWithName + 425
        47  python3                             0x000000010cd29ad3 PyRun_InteractiveOneObjectEx + 1123
        48  python3                             0x000000010cd26791 PyRun_InteractiveLoopFlags + 433
        49  python3                             0x000000010cd265bb PyRun_AnyFileExFlags + 139
        50  python3                             0x000000010cd4db82 Py_Main + 4690
        51  python3                             0x000000010cb7ef59 main + 313
        52  libdyld.dylib                       0x00007fff78427015 start + 1
        53  ???                                 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
Abort trap: 6

最初,なんだこれ!?ってなりました.

解決策

おそらく後ろで走っているプログラムによって解決策が異なるのかなと思います.
とりあえず2つあげときます.

解決策1

設定ファイルを変更

  1. PyQt5をインストール:pip install PyQt5
  2. ~/.matplotlib/matplotlibrcbackend:Qt4Aggを書き込む

解決策2

コードにmatplotlib.use("TkAgg")を入れる.

import matplotlib
matplotlib.use("TkAgg")

その他

Backendに設定できる値

ValueError: Unrecognized backend string 'qtagg': valid strings are \
 ['GTK3Agg', 'GTK3Cairo', 'MacOSX', 'nbAgg', 'Qt4Agg', 'Qt4Cairo', 'Qt5Agg', 'Qt5Cairo', 'TkAgg', 'TkCairo', 'WebAgg', 'WX', 'WXAgg', 'WXCairo', 'agg', 'cairo', 'pdf', 'pgf', 'ps', 'svg', 'template']

解決の途中こんなエラーが出てました.backendに設定できるリストっぽいですね.

Interactive Framework

ImportError: Cannot load backend 'TkAgg' \
 which requires the 'tk' interactive framework, as 'qt5' is currently running

解決策1の後に解決策2を試したら上記のエラーがでました.後ろで走っているインタラクティブフレームワークによって設定できる値を変えないといけないのかな?

参考

  1. Mac上のmatplotlibでグラフ描画できないときの対処法
  2. Matplotlib Crashing tkinter Application
3
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?