Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get matplotlib.pyplot working in virtual environment on OSX?

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

like image 362
PDN Avatar asked Jan 06 '23 15:01

PDN


1 Answers

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

like image 64
Jorge Aurelio Menendez Avatar answered Jan 10 '23 06:01

Jorge Aurelio Menendez