Python3 matplotlibでRuntimeErrorが出た時の対処

Deep Learningを知ろうと思い立ち、以下を読み進めている。

ゼロから作るDeep Learning ―Pythonで学ぶディープラーニングの理論と実装
斎藤 康毅
オライリージャパン
売り上げランキング: 108

この本ではAnacondaディストリビューションをお勧めしているが、pyenvを使っているので、pipで対応した。

使うライブラリは2つ。

  • numpy
  • matplotlib

次のコマンドでインストールできる。

pip install numpy matplotlib

そして、最初にmatplotlibを使ったコード、

import numpy as np
import matplotlib.pyplot as plt

x = np.arange(0, 6, 0.1)
y = np.sin(x)
plt.plot(x, y)
plt.show()

で次のようなエラーが出た。

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.

バックエンドが対応してないよ、というエラー。 メッセージで検索すると、osx - matplotlib: RuntimeError: Python is not installed as a framework - Stack Overflowを見つけた。

backend : TkAggと記載された、matplotlibrcを、~/.matplotlibに作成すればいい。

以下のコマンドでOK。

cd ~/.matplotlib
echo backend : TkAgg > matplotlibrc

これでエラーは出なくなった。ひとまず安心。

コメント

タイトルとURLをコピーしました