When I try the code of the gift that uses matplotlib
with python2
in the pyenv environment of Mac, it doesn't work.
From the result, install pyside2
, edit the config matplotlibrc
of matplotlib
, and
backend osx
To
backend qt5agg
Then it worked.
Mac OSX 10.15.5
Python 2.7.17
(installed with pyenv)
When I type python-code.py
(pseudonym), I get the following error.
Traceback (most recent call last):
File "python-code.py", line 60, in <module>
import matplotlib.pyplot as plt
File "/Users/username/.pyenv/versions/2.7.17/lib/python2.7/site-packages/matplotlib/pyplot.py", line 115, in <module>
_backend_mod, new_figure_manager, draw_if_interactive, _show = pylab_setup()
File "/Users/username/.pyenv/versions/2.7.17/lib/python2.7/site-packages/matplotlib/backends/__init__.py", line 63, in pylab_setup
[backend_name], 0)
File "/Users/username/.pyenv/versions/2.7.17/lib/python2.7/site-packages/matplotlib/backends/backend_macosx.py", line 17, in <module>
from matplotlib.backends import _macosx
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.
When you read
from matplotlib.backends import _macosx
As you can see, the GUI library seems to be useless.
When I looked it up, it seemed like a common error, and I came up with some solutions.
It seems to work with qt5agg
using Qt
, but another difficulty.
It seems that PyQt5
, which is familiar in python3
, cannot be used in python2
.
There seems to be an alternative, called pyside2
.
Put this in.
pip install pyside2
And
python -c "import matplotlib;print(matplotlib.matplotlib_fname())"
Then, check the path of matplotlibrc
(matplot config file), line 42
backend : macosx
Comment out and add qt5agg
.
# backend : macosx
backend : qt5agg
With this, the intended operation was performed safely. Congratulations.
I tried Tkagg
which uses Tk
, but it didn't work.
https://qiita.com/masatomix/items/03419c7ea10262da18f3 https://stackoverflow.com/questions/57238618/how-do-you-display-a-figure-in-matplotlib-using-pyside2
Recommended Posts