简体   繁体   中英

%matplotlib qt does not work

Python 2.7.12 |Anaconda 4.2.0 (64-bit)| Spyder 3.0.1| IPython 5.1.0 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

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

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!!!!

通过将matplotlib从matplotlib-1.5.3版本升级到matplotlib-2.2.2,我已经解决了这个问题

pip install matplotlib --upgrade

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 .

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