LoginSignup
2
1

More than 5 years have passed since last update.

matplotlibでのエラー、 plt.show()が動かない

Last updated at Posted at 2017-12-17

環境

macOS High Sierra
Python 2.7.10

エラー内容

matplotlibを使ってて下記が動かない泣



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.

Pythonでmatplotlibをimportするとエラーが出る場合の対処策(Mac)

おそらく原因はこれだと思う。backend設定がMacOSXになってる。
どうやらこれをTKAggに変えれば良いみたい!
しかしここからが大変、matplotlibrcが書き換えられない。。。。

解決法

backend設定を書き換えようとするも出来ない。。。権限ないのか?

そこで、
初心者向け MacでOperation not permittedの解決方法
を参考にして再起動するもリカバリーモードが出ない。しかし、なぜかグラフが書けた泣

一応確認でmatplotlibrc を見てみる。
因みに場所は、terminalで以下を叩く


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

で調べる事が出来て
僕はここにあった
/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/matplotlib/mpl-data/matplotlibrc

で肝心のmatplotlibrcは、


backend      : MacOSX

まんまやないかい!
なんでグラフ描けるようになったのかな?(再起動したからか?)

その後結局、今度はanimationが動かなかったのでエラーの指示通りコードの前に


import matplotlib
matplotlib.use('TKAgg')

書いたらanimationも動くようになった。
その時のエラー文はこれ


matplotlib.animation.BackendError: The current backend is 'MacOSX'
and may go into an infinite loop with blit turned on.  Either
turn off blit or use an alternate backend, for example, like
'TKAgg', using the following prepended to your source code:

import matplotlib
matplotlib.use('TKAgg')

デフォルトの設定ではないのでいちいち書かないといけないが一応動いた。でも、この指示文出るなら最初っから出してーー泣

2
1
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
2
1