When matplotlib doesn't work with python2.7

Overview

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.

environment

Mac OSX 10.15.5 Python 2.7.17 (installed with pyenv)

error contents

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.

solution

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.

Supplement

I tried Tkagg which uses Tk, but it didn't work.

reference

https://qiita.com/masatomix/items/03419c7ea10262da18f3 https://stackoverflow.com/questions/57238618/how-do-you-display-a-figure-in-matplotlib-using-pyside2

Recommended Posts

When matplotlib doesn't work with python2.7
When send_keys doesn't work
When dropbox-sdk-python doesn't work
Heatmap with Python + matplotlib
[Python] Customize Colormap when drawing graphs with matplotlib
[Python] Why pserve doesn't work
Error when playing with python
Search engine work with python
Create plot animation with Python + Matplotlib
A python graphing manual with Matplotlib.
[Python] font family and font with matplotlib
Heatmap with Dendrogram in Python + matplotlib
Draw Lyapunov Fractal with Python, matplotlib
Lognormal probability plot with Python, matplotlib
When using MeCab with virtualenv python
Precautions when using six with Python 2.5
Intellisense doesn't work with tensorflow2.0 + VScode
[Python] Format when to_csv with pandas
Articles to read when Blender Python script code doesn't work in 2.80
Pip doesn't work with PyDev in Pleiades
[Python] Set the graph range with matplotlib
Snippet when searching all bits with python
Note when creating an environment with python
Precautions when solving DP problems with Python
How to work with BigQuery in Python
Question: Multiple integrals with python don't work
To work with timestamp stations in Python
FizzBuzz with Python3
Scraping with Python
Statistics with python
Scraping with Python
Python with Go
#Python basics (#matplotlib)
Twilio with Python
Integrate with Python
Play with 2016-Python
AES256 with python
Tested with Python
python starts with ()
Animation with matplotlib
with syntax (Python)
Bingo with python
Zundokokiyoshi with python
Animation with matplotlib
Histogram with matplotlib
Animate with matplotlib
Excel with Python
Microcomputer with Python
Cast with python
Tensorflow doesn't work!
Error when installing a module with Python pip
Recommended environment and usage when developing with Python
[Python] limit axis of 3D graph with Matplotlib
Read Python csv data with Pandas ⇒ Graph with Matplotlib
Personal tips when doing various things with Python 3
When I try matplotlib in Python, it says'cairo.Context'
[Python] How to draw multiple graphs with Matplotlib
pyenv doesn't work with tcl-tk installed by homebrew.
Precautions when dealing with control structures in Python 2.6
Investigation when import cannot be done with python
A memo when creating a python environment with miniconda