テクめも

プログラミング関連のちょっとしたTipsなどを書いています。

MacのPython仮想環境でmatplotlibを使うときのエラーを解決するワンライナー

事象

python -m venv .vencとかで作った仮想環境でMatplotlibを使おうとするとエラーが発生する

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.

解決策

matplotlibrcの38行目のbackend : macosxbackend : Tkaggに変更する

sed -i -e 's/backend: macosx/backend: Tkagg/g' $(python -c "import matplotlib;print(matplotlib.matplotlib_fname())")

参考

pyenvとvirtualenvで環境構築した時にmatplotlib.pyplotが使えなかった時の対処法 - Qiita