Teodor Petrut
Teodor Petrut

Reputation: 63

matplotlib does not import PyQt4, PyQt5 or PySide

I'm using Canopy 2.1.3 distribution (on Ubuntu 16.04) which as you may know contains the matplotlib package. Simply running in the Canopy's shell import matplotlib.pyplot as plt gives me

ImportError: Matplotlib qt-based backends require an external PyQt4, PyQt5, or PySide package to be installed, but it was not found.

I have installed all of the three packages with Synaptic but the problem persists. Could this be related to environment variables? Or, could it be because Canopy's Python 3.5 and Ubuntu's Python 2.7 are conflicting?

EDIT: I have finally used the PyQt by installing it from the Canopy's package manager.

Upvotes: 3

Views: 7043

Answers (2)

Jonathan March
Jonathan March

Reputation: 5810

From the user guide:

Known Issue: In Canopy running Python 3, GUI backend must be explicitly set

If you ask matplotlib to display a plot, or otherwise run Python code that creates a GUI window, by default you will get an error traceback ending with: ImportError: No module named 'PyQt4'

The solutions are to install PyQt (which is GPL-licensed) in the Canopy Package Manager, or to use the Canopy Preference menu’s Python tab to specify the TK PyLab backend for creating GUIs. For details, see “Python 3 in Canopy 2 - ‘No module named PyQt4’ error”

Upvotes: 2

Reblochon Masque
Reblochon Masque

Reputation: 36662

It is likely that the default backend for matplotlib.pyplot is set to qt.
I have very little experience with canopy, but it should be possible to change the default to another back end; maybe someone else will be able to tell you how, or you can find it by yourself.

In the meantime, you can try to add this above all other matplotlib imports:

import matplotlib
matplotlib.use('TkAgg')

Upvotes: 6

Related Questions