matplotlib不会导入PyQt4,PyQt5或PySide

matplotlib不会导入PyQt4,PyQt5或PySide

问题描述:

我正在使用Canopy 2.1.3发行版(在Ubuntu 16.04上),正如您可能知道的那样,它包含matplotlib软件包。只需在雨棚的外壳import matplotlib.pyplot as plt运行给我matplotlib不会导入PyQt4,PyQt5或PySide

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

我已经安装了所有的三个包立得的,但问题仍然存在。 这可能与环境变量有关吗?或者,可能是因为Canopy的Python 3.5和Ubuntu的Python 2.7有冲突吗?

编辑: 我终于通过从Canopy的包管理器安装它来使用PyQt。

+0

问题和答案应该是未来的读者有用。所以我会邀请你,而不是在问题中给出一个单句的答案,提供一个完整的答案和更多的细节,以便将来有同样问题的其他人可以从中受益。 – ImportanceOfBeingErnest

很可能matplotlib.pyplot的默认后端设置为qt
我对冠层的经验很少,但应该可以将默认值更改为另一个后端;也许别人可以告诉你如何,或者你可以自己找到它。

在此期间,您可以尝试添加这一切其他matplotlib进口上面:

import matplotlib 
matplotlib.use('TkAgg') 

从用户指南:

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”