%matplotlib qt 以错误结束 - 调用 PyQt5 而不是 PyQt4

%matplotlib qt ends in error - calls PyQt5 instead of PyQt4

我正在尝试在 ipython/spyder 中使用命令 %matplotlib qt 在新 window 中打开图形,但它显示以下错误

%matplotlib qt
Traceback (most recent call last):

File "<ipython-input-1-4460ac8d7abe>", line 1, in <module>
get_ipython().magic(u'matplotlib qt')

File "/usr/lib/python2.7/site-packages/IPython/core/interactiveshell.py", line 2160, in magic
return self.run_line_magic(magic_name, magic_arg_s)

 File "/usr/lib/python2.7/site-packages/IPython/core/interactiveshell.py", line 2081, in run_line_magic
result = fn(*args,**kwargs)

 File "<decorator-gen-105>", line 2, in matplotlib

 File "/usr/lib/python2.7/site-packages/IPython/core/magic.py", line 188, in <lambda>
call = lambda f, *a, **k: f(*a, **k)

 File "/usr/lib/python2.7/site-packages/IPython/core/magics/pylab.py", line 100, in matplotlib
gui, backend = self.shell.enable_matplotlib(args.gui)

 File "/usr/lib/python2.7/site-packages/IPython/core/interactiveshell.py", line 2950, in enable_matplotlib
pt.activate_matplotlib(backend)

 File "/usr/lib/python2.7/site-packages/IPython/core/pylabtools.py", line 309, in activate_matplotlib
matplotlib.pyplot.switch_backend(backend)

 File "/usr/lib64/python2.7/site-packages/matplotlib/pyplot.py", line 231, in switch_backend
_backend_mod, new_figure_manager, draw_if_interactive, _show = pylab_setup()

 File "/usr/lib64/python2.7/site-packages/matplotlib/backends/__init__.py", line 60, in pylab_setup
[backend_name], 0)

 File "/usr/lib64/python2.7/site-packages/matplotlib/backends/backend_qt5agg.py", line 16, in <module>
from .backend_qt5 import (

 File "/usr/lib64/python2.7/site-packages/matplotlib/backends/backend_qt5.py", line 18, in <module>
import matplotlib.backends.qt_editor.figureoptions as figureoptions

 File "/usr/lib64/python2.7/site-packages/matplotlib/backends/qt_editor/figureoptions.py", line 20, in <module>
import matplotlib.backends.qt_editor.formlayout as formlayout

 File "/usr/lib64/python2.7/site-packages/matplotlib/backends/qt_editor/formlayout.py", line 56, in <module>
from matplotlib.backends.qt_compat import QtGui, QtWidgets, QtCore

 File "/usr/lib64/python2.7/site-packages/matplotlib/backends/qt_compat.py", line 137, in <module>
from PyQt5 import QtCore, QtGui, QtWidgets

ImportError: No module named PyQt5

我没有安装PyQt5,而是在centos 7.4安装了PyQt4。还修改了 file matplotlibrc (/usr/lib64/python2.7/site-packages/matplotlib/mpl-data/matplotlibrc) 文件更改后端如下:

 # If you omit this parameter, it will always default to "Agg", which is a
 # non-interactive backend.
 backend      : Qt4Agg

 # If you are using the Qt4Agg backend, you can choose here
 # to use the PyQt4 bindings or the newer PySide bindings to
 # the underlying Qt4 toolkit.
 backend.qt4 : PyQt4        # PyQt4 | PySide

但它仍然调用 PyQt5。在运行命令“%matplotlib qt”后,后端变回Qt5Agg(打印matplotlib.rcParams['backend']),甚至我在matplotlibrc中写了Qt4Agg。

注意:- 我也试过“%matplotlib qt4”

Warning: Cannot change to a different GUI toolkit: qt4. Using qt instead.

请参考IPython 5 configuration page。我没有设置 IPython 5 和 Python 2.7,所以我不能给你确切的配置代码。但是为了 IPython 使用 Qt4,您必须遵循以下步骤:

  1. 像在 this 页面中那样创建配置文件。
  2. 您需要更改的配置列表在 this 页面上给出。只需浏览列表,只要看到 qt4 和 qt5 之间的选项,只需在步骤 1 中创建的配置文件中将这些值设置为 qt4。

希望对您有所帮助。