%matplotlib qt不起作用

时间:2016-11-13 06:05:27

标签: python matplotlib pyqt4

Python 2.7.12 |Anaconda 4.2.0 (64-bit)| Spyder 3.0.1| IPython 5.1.0(在Windows上运行)

通过以下方式在新窗口中询问地块时:

%matplotlib qt

我收到错误:

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 个答案:

答案 0 :(得分:4)

我认为您的问题是由QT_API环境变量设置为pyqt4(或pyside)引起的。例如,通过在Python提示符中键入import os; print(os.environ.get('QT_API'))来检查这一点。

如果是这种情况,您需要将其删除或将其设置为pyqt5。谷歌如果你不知道如何在Windows下设置环境变量。

答案 1 :(得分:3)

我使用了import os; print(os.environ.get(&#39; QT_API&#39;)),然后我获得了pyqt5。那么,我去了工具 - &gt;偏好 - &gt; Ipython控制台 - &gt;图形和我选择了后端Qt5,重启spyder后,瞧,问题解决了。感谢!!!!

答案 2 :(得分:2)

通过将matplotlib从matplotlib-1.5.3升级到matplotlib-2.2.2,已经为我解决了此问题

pip install matplotlib --upgrade

答案 3 :(得分:1)

如果你想在Spyder的新窗口中显示你的情节,你就不需要%matplotlib qt

相反,请转到Spyder中的ToolsPreferences,然后在Preferences点击IPython consol。在IPython consol点击标签Graphics,然后点击Graphics Backend下拉列表更改InlineQT