简体   繁体   中英

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.

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

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”

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM