PDN PDN - 1 year ago 246
Python Question

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,
_backend_mod, new_figure_manager, draw_if_interactive, _show = pylab_setup()

line 32, in pylab_setup

line 18, in
from .backend_qt5agg import FigureCanvasQTAggBase as _FigureCanvasQTAggBase

line 15, in
from .backend_qt5 import QtCore

line 31, in
from .qt_compat import QtCore, QtGui, QtWidgets, _getSaveFileName, version

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

Answer Source

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

source deactivate
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download