mranvick
mranvick

Reputation: 389

Which backend for matplotlib using MacOS?

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 :

Using the first one works well but I wonder:

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)?

Upvotes: 12

Views: 13319

Answers (3)

Brandt
Brandt

Reputation: 5639

There are two things you can try.

  • You can read Matplotlib info page on that, https://matplotlib.org/3.1.0/faq/osx_framework.html,

    The default python provided in (Ana)conda is not a framework build. However, a framework build can easily be installed, both in the main environment and in conda envs: install python.app (conda install python.app) and use pythonw rather than python.

    And follow the instructions.

  • Or simply follow the error message you get when you try %matplotlib inline,

    (...)
    UnknownBackend: No event loop integration for 'inline'. Supported event loops are: qt, qt4, qt5, gtk, gtk2, gtk3, tk, wx, pyglet, glut, osx
    

    I did %matplotlib osx and have plt.imshow(myimg) working just fine afterwards.

Upvotes: 0

Babatunde Adekunle
Babatunde Adekunle

Reputation: 191

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:

  • A Framework build - Quite important for GUI stuffs in MacOXs
  • A regular build.

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.

Upvotes: 3

Tyler Estes
Tyler Estes

Reputation: 59

I'm going to make some assumptions. If they're wrong I apologize.

  • You installed Python with Anaconda.

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.

Upvotes: 2

Related Questions