k0b0's record.

Computer Engineering, Arts and Books

Pythonでmatplotlibをimportするとエラーが出る(on macOS Mojave 10.14.1) 追記[@2018/12/04]

matplotlibをimportするとエラーが出る

以下がそのエラー内容である。

from matplotlib.backends import _macosx
RuntimeError: Python is not installed as a framework. The Mac OS X backend will not be able to
function correctly if Python is not installed as a framework. See the Python documentation for 
more information on installing Python as a framework on Mac OS X. Please either reinstall Python 
as a framework, or try one of the other backends. If you are using (Ana)Conda please install 
python.app and replace the use of 'python' with 'pythonw'. See 'Working with Matplotlib on OSX' 
in the Matplotlib FAQ for more information.

解決方法

エラー内容を見てみると、macOSのバックエンドに問題があるみたい。
なので、matplotlibrcの場所を特定して内容を書き換える。

matplotlibrcの場所を特定する

python -c "import matplotlib;print(matplotlib.matplotlib_fname())"

matplotlibrcをエディタで開き

backend : macosx

の行を以下に書き換えればOK。

backend : Tkagg

上記の方法でうまくいかなかった場合

Qtを使ってみる

以下のコマンドよりPyQt5をインストール。

pip install PyQt5

matplotlibrcを編集してバックエンドをQt4Aggにする。
(matplotlibrcの場所は「python -c "import matplotlib;print(matplotlib.matplotlib_fname())"」で確認する。)

backend : Qt4Agg

動作確認

python3.7.0で正常に動作した。