Help us understand the problem. What is going on with this article?

MatplotlibでRuntimeError,ImportErrorが出てきた時の対処法

More than 1 year has passed since last update.

MatplotlibでRuntimeError,ImportErrorになった時の対処法


経緯

Pythonの学習を進めている途中で以下のようなエラーにぶち当たり、色々試行錯誤して理解しました。せっかくなのでそれを解決するに至ったストーリーをQiitaの練習がてら残します。。。

該当のコード

import matplotlib.pyplot as plt
import numpy as np

x = np.arange(0,10,0.1)
print(x)

y = 2 * x + 1
print(y)

plt.plot(x,y)
plt.show()

エラー

/Users/MYNAME/.pyenv/versions/anaconda3-5.1.0/envs/Pycharm_test/bin/python /Users/MYNAMEo/math_training/liner_function
Traceback (most recent call last):
  File "/Users/MYNAME/math_training/liner_function", line 3, in <module>
    import matplotlib.pyplot as plt
  File "/Users/MYNAME/.pyenv/versions/anaconda3-5.1.0/envs/Pycharm_test/lib/python3.6/site-packages/matplotlib/pyplot.py", line 2371, in <module>
    switch_backend(rcParams["backend"])
  File "/Users/MYNAME/.pyenv/versions/anaconda3-5.1.0/envs/Pycharm_test/lib/python3.6/site-packages/matplotlib/pyplot.py", line 207, in switch_backend
    backend_mod = importlib.import_module(backend_name)
  File "/Users/MYNAME/.pyenv/versions/anaconda3-5.1.0/envs/Pycharm_test/lib/python3.6/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "/Users/MYNAME/.pyenv/versions/anaconda3-5.1.0/envs/Pycharm_test/lib/python3.6/site-packages/matplotlib/backends/backend_macosx.py", line 14, in <module>
    from matplotlib.backends import _macosx
ImportError: 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 framework under macosx?

要約すると

「backend」っていう設定が行える「設定ファイル(CONFIGURATION FILE)」なるものがあるらしい。。。その設定によっては今回のようなエラーが起こるそうな

backendはどうやって設定する?

この設定ファイルをあーせいこーせい言うブログがいくつか見つかったのですがそもそも「設定ファイル」がどこにあってどう操作するのかわかんないもんだから少々戸惑いましたが、

つまりこうするらしいです↓

1.まず設定ファイルの場所をpythonで以下の処理を行い発見する。

import matplotlib as mpl

print(mpl.get_configdir())
print(mpl.matplotlib_fname())

すると以下のような出力になるはず

~/matplotlib/mpl-data/matplotlibrc

なるほど!このmpl-dataってディレクトリに「設定ファイル(=matplotlibrc)」が入っているのですね!!!

2.ターミナルで設定ファイルを操作する。

1の出力結果を見ながらmpl-dataまでたどり着いたら、以下の操作

$ open matplotlibrc

すると、ついに設定ファイル(.txt)が姿を表します!
こんな感じ

### MATPLOTLIBRC FORMAT

# This is a sample matplotlib configuration file - you can find a copy
# of it on your system in
# site-packages/matplotlib/mpl-data/matplotlibrc.  If you edit it
# there, please note that it will be overwritten in your next install.
# If you want to keep a permanent local copy that will not be
# overwritten, place it in the following location:
# unix/linux:
#     $HOME/.config/matplotlib/matplotlibrc or
#     $XDG_CONFIG_HOME/matplotlib/matplotlibrc (if $XDG_CONFIG_HOME is set)
# other platforms:
#     $HOME/.matplotlib/matplotlibrc
#
# See http://matplotlib.org/users/customizing.html#the-matplotlibrc-file for
# more details on the paths which are checked for the configuration file.
#
# This file is best viewed in a editor which supports python mode
# syntax highlighting. Blank lines, or lines starting with a comment
# symbol, are ignored, as are trailing comments.  Other lines must
# have the format
#    key : val # optional comment
#
# Colors: for the color values below, you can either use - a
# matplotlib color string, such as r, k, or b - an rgb tuple, such as
# (1.0, 0.5, 0.0) - a hex string, such as ff00ff - a scalar
# grayscale intensity such as 0.75 - a legal html color name, e.g., red,
# blue, darkslategray

#### CONFIGURATION BEGINS HERE

# The default backend; one of GTK GTKAgg GTKCairo GTK3Agg GTK3Cairo
# MacOSX Qt4Agg Qt5Agg TkAgg WX WXAgg Agg Cairo GDK PS PDF SVG
# Template.
# You can also deploy your own backend outside of matplotlib by
# referring to the module name (which must be in the PYTHONPATH) as
# 'module://my_backend'.
backend      : なんちゃら

#まだまだ続く。。。

この最後の行の 
backend : なんちゃら
を、

backend : TkAgg

にしてあげる。保存して終わり。

keroido
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした