The question of using matplotlib
with MacOS is a tricky one which has already been thoroughly reviewed by a number of discussions (see below). The problem is the following:
Here is the simplest code snippet I came up with which allows reproducing the issue:
from matplotlib import pyplot as plt
x = [1, 2, 3]
y = [1, 2, 3]
plt.plot(x, y)
plt.show()
This throws the following error:
2019-03-22 12:25:43.429 python3.7[22209:554135] -[NSApplication _setup:]: unrecognized selector sent to instance 0x7f85866b9de0
2019-03-22 12:25:43.431 python3.7[22209:554135] \*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSApplication _setup:]: unrecognized selector sent to instance 0x7f85866b9de0'
*** First throw call stack:([...])
libc++abi.dylib: terminating with uncaught exception of type NSException
Process finished with exit code 134 (interrupted by signal 6: SIGABRT)
The issue is documented here. One solution is to install the PyQt5
package to your Python installation and to add the following lines at the beginning of your script:
import matplotlib
matplotlib.use("Qt5Agg")
While this works perfectly well, I am wondering why other backends fail to provide similar behavior.
Indeed I tried using MacOSX backend :
import matplotlib
matplotlib.use('MACOSX')
Which yields to the error:
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.
The issue is documented here, there and in plenty of other threads.
Two solutions came out :
python.app
(conda install python.app
) and launch your script with pythonw
instead of python
TKAgg
backendUsing the first one works well but I wonder:
pythonw
instead of python
?python.app
package ?As for the second one, it does "work" up to a certain point: when running matplotlib
using TkAgg
, the plot window is really buggy. Indeed, it often needs several clicks on the "zoom", "pan" or "home" buttons to get them to actually work. It really is a great pain to use it. I asked several colleagues or friends using matplotlib
with TkAgg
and they all have the same issue.
Does anyone know the reason for this state of fact? Or if there is any workaround to avoid this issue (apart from installing pyqt5)?
Matplotlib is a plotting library. It relies on some backend to actually render the plots. The default backend is the agg backend.
If you are using Python from https://www.python.org, Homebrew, or Macports, then you can use the standard pip installer to install Matplotlib binaries in the form of wheels. ( sudo python3. 6 ... on Macports). You might also want to install IPython or the Jupyter notebook ( python3 -mpip install ipython notebook ).
Using the first option is your best bet since you are already working with a virtual environment. According to matplotlib, there are two variants of python:
Matplotlib in this case would want to interact natively with OSX and for this, it needs the Framework build this is the reason why installing the python.app
type of python is important.
More information can be gotten from Matplotlib FAQ.
Check this link for more about the need for a framework build python.
I'm going to make some assumptions. If they're wrong I apologize.
Personally, I've never had any problems on mac with matplotlib. My setup is: Mojave, Python3.7.3 in a venv using the python built in module (python3 -m venv
), and matplotlib 3.0.3.
I can't answer your question on how to fix your problem, but I'm kind of trying to answer your "is there any workaround" question. Personally, I've always had issues with using Anaconda/Spyder/Conda for Python. I've always felt installing it as its own binary/app on the system leads to the fewest errors.
Now, I'm not saying you have to download and install by hand though. I use homebrew and it saves me headaches everyday I assume (such as upgrading applications and packages). That's the "work around" I'd suggest. Because isn't installing via Anaconda/Spyder already a workaround to installing Python properly? I've always felt performing one work around requires more workarounds for full functionality. Such as having to specify the matplotlib backend when it should be detected by default.
Obviously, I'm a little biased against that tool and that may be reflected in this answer, so take it with a grain of salt. Even though Conda is a legitimate tool that I think is useful, I find it annoying having to use both pip and conda when conda doesn't contain the packages I want.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With