简体   繁体   中英

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

I am trying to open graphics in a new window using the command %matplotlib qt in ipython/spyder, but it shows following error

%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

I didn't install PyQt5, but installed PyQt4 in centos 7.4. Also modified the file matplotlibrc (/usr/lib64/python2.7/site-packages/matplotlib/mpl-data/matplotlibrc) file to change backend and as follows:

 # 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

But still it calls PyQt5. After running the command "%matplotlib qt", the back-end changed back into Qt5Agg (print matplotlib.rcParams['backend']), even I wrote Qt4Agg in matplotlibrc.

Note:- I tried "%matplotlib qt4" also

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

Please refer to the IPython 5 configuration page . I don't have a IPython 5 and Python 2.7 set up so I cannot give you the exact configuration code. But inorder to make IPython use Qt4 you will have to follow the following steps:

  1. Create configuration files like giving in this page.
  2. The list of configurations that you will need to change are given on this page. Just go through the list and wherever you see an option between qt4 and qt5 just set those values to qt4 in the config files that you created in step 1.

Hope that helps.

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