%matplotlib qt does not work

2020-06-06 05:06发布

Python 2.7.12 |Anaconda 4.2.0 (64-bit)| Spyder 3.0.1| IPython 5.1.0 (Running on Windows)

When asking for plots in a new window via:

%matplotlib qt

I get an error:

Traceback (most recent call last):

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

  File "C:\Anaconda2\lib\site-packages\IPython\core\interactiveshell.py", line 2158, in magic
    return self.run_line_magic(magic_name, magic_arg_s)

  File "C:\Anaconda2\lib\site-packages\IPython\core\interactiveshell.py", line 2079, in run_line_magic
    result = fn(*args,**kwargs)

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

  File "C:\Anaconda2\lib\site-packages\IPython\core\magic.py", line 188, in <lambda>
    call = lambda f, *a, **k: f(*a, **k)

  File "C:\Anaconda2\lib\site-packages\IPython\core\magics\pylab.py", line 100, in matplotlib
    gui, backend = self.shell.enable_matplotlib(args.gui)

  File "C:\Anaconda2\lib\site-packages\IPython\core\interactiveshell.py", line 2947, in enable_matplotlib
    pt.activate_matplotlib(backend)

  File "C:\Anaconda2\lib\site-packages\IPython\core\pylabtools.py", line 295, in activate_matplotlib
    matplotlib.pyplot.switch_backend(backend)

  File "C:\Anaconda2\lib\site-packages\matplotlib\pyplot.py", line 232, in switch_backend
    _backend_mod, new_figure_manager, draw_if_interactive, _show = pylab_setup()

  File "C:\Anaconda2\lib\site-packages\matplotlib\backends\__init__.py", line 32, in pylab_setup
    globals(),locals(),[backend_name],0)

  File "C:\Anaconda2\lib\site-packages\matplotlib\backends\backend_qt4agg.py", line 18, in <module>
    from .backend_qt5agg import FigureCanvasQTAggBase as _FigureCanvasQTAggBase

  File "C:\Anaconda2\lib\site-packages\matplotlib\backends\backend_qt5agg.py", line 16, in <module>
    from .backend_qt5 import QtCore

  File "C:\Anaconda2\lib\site-packages\matplotlib\backends\backend_qt5.py", line 31, in <module>
    from .qt_compat import QtCore, QtGui, QtWidgets, _getSaveFileName, __version__

  File "C:\Anaconda2\lib\site-packages\matplotlib\backends\qt_compat.py", line 137, in <module>
    from PyQt4 import QtCore, QtGui

ImportError: No module named PyQt4

4条回答
放我归山
2楼-- · 2020-06-06 05:36

I think your issue is caused by having a QT_API environment variable that still is set to pyqt4 (or pyside). Check this, for instance, by typing import os; print(os.environ.get('QT_API')) in the Python prompt.

If this is the case you need to remove it or set it to pyqt5. Google how to set an environment variable under Windows if you don't know how.

查看更多
爷的心禁止访问
3楼-- · 2020-06-06 05:36

If you want your plots in a new window in Spyder, you don't need the %matplotlib qt.

Instead, go to Tools and Preferences in Spyder, then in Preferences click on IPython consol. In IPython consol click the tab Graphics, then in the Graphics Backend drop-down change Inline to QT.

查看更多
在下西门庆
4楼-- · 2020-06-06 05:50

This issue has been resolved for me by upgrading matplotlib from version matplotlib-1.5.3 to matplotlib-2.2.2

pip install matplotlib --upgrade
查看更多
叛逆
5楼-- · 2020-06-06 05:51

I used import os; print(os.environ.get('QT_API')), and then I obtained pyqt5. So then, I went to Tools -> preferences -> Ipython console -> graphics and I selected backend Qt5, and after restarting spyder, voila, problem solved. Thanks!!!!

查看更多
登录 后发表回答