4

I first came across the answer to this question where I found out I needed to install my own backend framework. Since the answer mentioned PyQt4, I chose to go with that. Following links in the doc, I eventually ended up downloaded SIP (pre-req for PyQt4) and then PyQt4 from here. Finally, in my code, I have:

import matplotlib
matplotlib.use('Qt4agg') # need to call use() before importing plt
import matplotlib.pyplot as plt

However I'm still getting this error:

Traceback (most recent call last): File ".../venv/lib/python3.5/site-packages/matplotlib/backends/qt_compat.py", line 159, in from PySide import QtCore, QtGui, version, version_info ImportError: No module named 'PySide'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):

File ".../program.py", line 7, in import matplotlib.pyplot as plt File ".../venv/lib/python3.5/site-packages/matplotlib/pyplot.py", line 114, in _backend_mod, new_figure_manager, draw_if_interactive, _show = pylab_setup()

File ".../venv/lib/python3.5/site-packages/matplotlib/backends/init.py", line 32, in pylab_setup globals(),locals(),[backend_name],0)

File ".../venv/lib/python3.5/site-packages/matplotlib/backends/backend_qt4agg.py", line 18, in from .backend_qt5agg import FigureCanvasQTAggBase as _FigureCanvasQTAggBase

File ".../venv/lib/python3.5/site-packages/matplotlib/backends/backend_qt5agg.py", line 15, in from .backend_qt5 import QtCore

File ".../venv/lib/python3.5/site-packages/matplotlib/backends/backend_qt5.py", line 31, in from .qt_compat import QtCore, QtGui, QtWidgets, _getSaveFileName, version

File ".../venv/lib/python3.5/site-packages/matplotlib/backends/qt_compat.py", line 162, in "Matplotlib qt-based backends require an external PyQt4, PyQt5,\n" ImportError: Matplotlib qt-based backends require an external PyQt4, PyQt5, or PySide package to be installed, but it was not found.

Has anyone experienced this before? Any debugging advice or help on where to go from here?

EDIT: I'll add that trying to import PyQt4 from my virtual environment works, so I'm not sure why matplotlib isn't finding it...

EDIT2: Not sure if it matters but I'm using PyCharm

Community
  • 1
  • 1
PDN
  • 771
  • 2
  • 13
  • 29
  • 1
    "Matplotlib qt-based backends require an external PyQt4, PyQt5, or PySide package to be installed, but it was not found." You need to install the Qt framework, or use a different backend (`TkAgg` generally works). –  Feb 18 '16 at 05:03
  • @Evert I've installed PyQt4 (Qt4Agg) but it can't seem to find it. I can import it fine in the python interpretter, so is there anything extra that you can think of that I've forgotten to do? – PDN Feb 18 '16 at 05:15
  • I misunderstood (or misread, probably). What version is your matplotlib installation? Could you try upgrading it? –  Feb 18 '16 at 06:45
  • Subtle, but try with an extra capital in the name of the backend: `matplotlib.use('Qt4Agg') ` (capital A), instead of using `Qt4agg`. –  Feb 18 '16 at 06:48
  • @Evert I have matplotlib 1.5.1, Qt4Agg doesn't work either – PDN Feb 19 '16 at 00:55

4 Answers4

4

I struggled with this for days and have finally arrived at a pretty simple solution after looking through loads of different stack overflow posts.

This is my solution for MacOSX and Python3.X, for someone who has already installed python3 via homebrew (i.e. brew install python3) and has virtualenv installed and a virtual environment already created with python3 (e.g. via virtualenvwrapper, mkvirtualenv myvenv -p python3):

(1) Install pyqt and sip using brew:

    $ brew install sip --with-python3
    $ brew install pyqt --with-python3

(2) Link the pyqt and sip files installed in /usr/local/Cellar/ (the default location for homebrew installations) to the site-packages directory in your virtualenv:

    $ ln -s /usr/local/Cellar/sip/{SIPVERSION}/lib/python3.X/site-packages/*.* ~/{VIRTUALENVHOME}/{VIRTUALENVNAME}/lib/python3.X/site-packages/
    $ ln -s /usr/local/Cellar/pyqt/{PYQTVERSION}/lib/python3.X/site-packages/PyQt4/*.* ~/{VIRTUALENVHOME}/{VIRTUALENVNAME}/lib/python3.X/site-packages/PyQt4

Make sure to modify the text in curly brackets {} as needed for your system, where VIRTUALENVHOME is the path to your virtual environment, VIRTUALENVNAME is the name of it, and SIPVERSION and PYQTVERSION are the versions of sip and pyqt that you downloaded. These can be found by simply looking in their corresponding directories /usr/local/Cellar/sip and /usr/local/Cellar/pyqt (the contents of these should be a single directory with the version number). Also don't forget to plug in the version of python3 you are using! (into all places where it says python3.X)

(3) pip install matplotlib in your virtualenv (if you haven't already) and modify the matplotlibrc so that matplotlib uses the PyQt4 backend instead of the default macosx backend:

    # activate your virtual environment
    $ workon VIRTUALENVNAME # if you are using virtualenvwrapper
    $ #source ~/{VIRTUALENVHOME}/{VIRTUALENVNAME}/bin/activate # if you are not using virtualenvwrapper
    # if you haven't already, install matplotlib
    $ pip install matplotlib
    # modify the matplotlibrc file to change the backend it is using
    $ nano ~/{VIRTUALENVHOME}/{VIRTUALENVNAME}/lib/python3.5/site-packages/matplotlib/mpl-data/matplotlibrc

This will open matplotlibrc in the default terminal text editor (alternatively you can use open -e instead of nano to open it in TextEdit). Go down to the first section after #### CONFIGURATION BEGINS HERE and change the line backend : macosx to backend : Qt4Agg. In the next section, uncomment the line backend.qt4 : PyQt4.

With this done, you should be up and running. You can test whether this worked with the following simple lines of code (assuming you have ipython installed in your virtual environment):

    $ ipython -pylab
    >>> import numpy as np
    >>> plot(np.arange(10))

A plot should appear with a straight line. If this doesn't work, try reinstalling matplotlib in your virtual environment (i.e. pip uninstall matplotlib and pip install matplotlib).

3

Resolved this issue following this:

pip install matplotlib There is a directory in you root called ~/.matplotlib.

Create a file ~/.matplotlib/matplotlibrc there and add the following code: backend: TkAgg

Supritha P
  • 826
  • 8
  • 7
2

Have you though about using conda instead of virtualenv?

conda create -n matplotenv matplotlib
source activate matplotenv

python -c "import matplotlib.pyplot as plt
p = plt.plot([1,2,3])
plt.show()"

source deactivate
maxymoo
  • 35,286
  • 11
  • 92
  • 119
  • I actually didn't know you could create virtual environments without `virtualenv`! I'll definitely give that a shot if all else fails since I already have my environment set up... is conda more popular / better in your opinion? – PDN Feb 18 '16 at 05:50
  • I've been a lot happier since I switched from python/virtualenv to Anaconda about 2 years ago ... it's very popular in the scientific/data-analysis community, and there are conda packages for a lot of things, you can `pip install` anything else you need. The best thing is support for non-python dependencies (like qt4). To quote from their website "Pip is a package manager, and Virtualenv is an environment manager. Conda is both." So this means that conda can look after things like qt4 which aren't part of Python, but are still dependencies for the packages that you want to run. – maxymoo Feb 18 '16 at 23:03
  • If you go down this road, you'll want to uninstall your current distribution of python, and download an installation binary from the anaconda website, i suspect you'll find it easier recreate your environment going than you might be worried about~ – maxymoo Feb 18 '16 at 23:05
  • well, I think I'm ready to go down this path now since I can't figure this bug out. As a new Mac OSX user, could you tell me how to uninstall Python? I think it came with 2.7 installed, and I installed 3.5 on it using the installer on www.python.org/downloads and then pip installed a bunch of stuff globally and on my user – PDN Feb 19 '16 at 01:29
  • take a look at http://stackoverflow.com/questions/3819449/how-to-uninstall-python-2-7-on-a-mac-os-x-10-6-4 ... for future reference, don't install things like that, use a package manager like `homebrew` instead, that makes uninstalling/upgrading much cleaner. – maxymoo Feb 19 '16 at 02:17
  • So, I ended up getting an answer here: http://stackoverflow.com/questions/21784641/installation-issue-with-matplotlib-python. I'm not entirely sure why it works because I'd imagine matplotlib gets installed in my venv, but guess not. Anyway I'm marking your answer accepted since no one should have to go through this and you've been super helpful. Thanks! – PDN Feb 19 '16 at 03:32
0

I had the same problem, and what I did actually solved it (it might help if you didn't understand everything Jorge said!) :
1. go on anaconda navigator and launch qtconsole
2. type :$ brew install sip --with-python3
$ brew install pyqt --with-python3 3. copy the result of it in python, and execute.
Now try making a graph, it should work!

Dr.jacky
  • 3,341
  • 6
  • 53
  • 91