LoginSignup
1
2

More than 5 years have passed since last update.

matplotlib.pyplotをimportするとエラーが出たり画面が白く描画される問題の対応(macOS)

Last updated at Posted at 2019-03-21

Graphillion を macOS Mojave & Python 3.7.2 で試す際に、得られたグラフを Graphillion のチュートリアル通りにプロットしようとしたのですが、 import matplotlib.pyplot as plt するときにエラーが出てしまいました

エラー内容

$ python
Python 3.7.2 (default, Mar 21 2019, 01:53:44)
[Clang 10.0.0 (clang-1000.10.44.4)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import matplotlib.pyplot as plt
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/mascii/.pyenv/versions/3.7.2/lib/python3.7/site-packages/matplotlib/pyplot.py", line 2372, in <module>
    switch_backend(rcParams["backend"])
  File "/Users/mascii/.pyenv/versions/3.7.2/lib/python3.7/site-packages/matplotlib/pyplot.py", line 207, in switch_backend
    backend_mod = importlib.import_module(backend_name)
  File "/Users/mascii/.pyenv/versions/3.7.2/lib/python3.7/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "/Users/mascii/.pyenv/versions/3.7.2/lib/python3.7/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.

対策

  • PyQt5pip install する
  • 以下のコマンドを実行して matplotlibrc の場所を特定する
$ python -c "import matplotlib;print(matplotlib.matplotlib_fname())"
  • vim や VSCode などで matplotlibrc を開く
  • backend: macosx となっている行を backend: Qt5Agg に置 き換える
    • backend: Tkagg とすると画面が白く描画されてしまいました Screen Shot 2019-03-21 at 19.04.36.png

以下のようなエラーが出ながらも、グラフをプロットができるようになりました。

/Users/mascii/.pyenv/versions/3.7.2/lib/python3.7/site-packages/networkx/drawing/nx_pylab.py:611: MatplotlibDeprecationWarning: isinstance(..., numbers.Number)
  if cb.is_numlike(alpha):

Screen Shot 2019-03-21 at 19.17.19.png

(上のグラフは以前に書いた Pythonとグラフ理論で全国小中学生プログラミング大会グランプリ作品を解く で紹介したプログラムで出力しました)

参考サイト

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