当我跑步时
import matplotlib.pyplot as plt
plt.plot([1,2,3,4])
我收到错误
Traceback (most recent call last):
File "<input>", line 1, in <module>
File "[...]/lib/python3.5/site-packages/matplotlib/pyplot.py", line 1203, in subplots
fig = figure(**fig_kw)
File "[...]/lib/python3.5/site-packages/matplotlib/pyplot.py", line 535, in figure
**kwargs)
File "[...]/lib/python3.5/site-packages/matplotlib/backends/backend_qt4agg.py", line 46, in new_figure_manager
return new_figure_manager_given_figure(num, thisFig)
File "[...]/lib/python3.5/site-packages/matplotlib/backends/backend_qt4agg.py", line 53, in new_figure_manager_given_figure
canvas = FigureCanvasQTAgg(figure)
File "[...]/lib/python3.5/site-packages/matplotlib/backends/backend_qt4agg.py", line 76, in __init__
FigureCanvasQT.__init__(self, figure)
File "[...]/lib/python3.5/site-packages/matplotlib/backends/backend_qt4.py", line 66, in __init__
QtWidgets.QWidget.__init__(self)
TypeError: __init__() missing 1 required positional argument: 'figure'
直到最近才开始工作。我不知道我在已安装的anaconda库中进行了任何更改,目前这些库(仅显示相关库):
matplotlib 2.0.0 np111py35_0 conda-forge
qt 5.6.2 3 defaults
qtconsole 4.2.1 py35_1 defaults
我该怎么做才能解决这个问题?
import matplotlib
我得到
Backend Qt4Agg is interactive backend. Turning interactive mode on.
Failed to enable GUI event loop integration for 'qt4'
Traceback (most recent call last):
File "[...]/PyCharm/pycharm-community-2016.3.2/helpers/pydev/_pydev_bundle/pydev_console_utils.py", line 563, in do_enable_gui
enable_gui(guiname)
File "[...]/PyCharm/pycharm-community-2016.3.2/helpers/pydev/pydev_ipython/inputhook.py", line 528, in enable_gui
return gui_hook(app)
File "[...]/PyCharm/pycharm-community-2016.3.2/helpers/pydev/pydev_ipython/inputhook.py", line 195, in enable_qt4
from pydev_ipython.inputhookqt4 import create_inputhook_qt4
File "[...]/PyCharm/pycharm-community-2016.3.2/helpers/pydev/_pydev_bundle/pydev_import_hook.py", line 21, in do_import
module = self._system_import(name, *args, **kwargs)
File "[...]/PyCharm/pycharm-community-2016.3.2/helpers/pydev/pydev_ipython/inputhookqt4.py", line 25, in <module>
from pydev_ipython.qt_for_kernel import QtCore, QtGui
File "[...]/PyCharm/pycharm-community-2016.3.2/helpers/pydev/_pydev_bundle/pydev_import_hook.py", line 21, in do_import
module = self._system_import(name, *args, **kwargs)
File "[...]/PyCharm/pycharm-community-2016.3.2/helpers/pydev/pydev_ipython/qt_for_kernel.py", line 85, in <module>
QtCore, QtGui, QtSvg, QT_API = load_qt(api_opts)
File "[...]/PyCharm/pycharm-community-2016.3.2/helpers/pydev/pydev_ipython/qt_loaders.py", line 281, in load_qt
api_options))
ImportError:
Could not load requested Qt binding. Please ensure that
PyQt4 >= 4.7 or PySide >= 1.0.3 is available,
and only one is imported per session.
Currently-imported Qt library: None
PyQt4 installed: False
PyQt5 installed: True
PySide >= 1.0.3 installed: True
Tried to load: ['pyqtdefault']
当我使用python script.py
运行我的脚本时,我不会发现上述消息,而是
[...]/lib/python3.5/site-packages/matplotlib/__init__.py:1401: UserWarning: This call to matplotlib.use() has no effect
because the backend has already been chosen;
matplotlib.use() must be called *before* pylab, matplotlib.pyplot,
or matplotlib.backends is imported for the first time.
答案 0 :(得分:1)
我猜想发生了类似下面的事情,虽然我无法确定,因为没有给出两个案例的确切代码。
第一个错误可能是在您(未注意)尝试使用未安装的后端时出现的。
import matplotlib #everthing works fine, no backend selected yet
matplotlib.use("TkAgg") #backend selected, would still be fine even if Tk wasnt installed.
import matplotlib.pyplot as plt # backend registered. Still not used.
plt.plot(..) # now the backend will be used. Clash!
# Backend cannot be used, since underlying library is missing
稍后你试图使用不同的后端。
import matplotlib #everthing works fine, no backend selected yet
matplotlib.use("TkAgg") #backend selected, would still be fine even if Tk wasnt installed.
import matplotlib.pyplot as plt # backend registered. Still not used.
....
import matplotlib
matplotlib.use("Qt5Agg") # backend "TkAgg" has already been registered.
# You cannot change it anymore.
这就是让你
的原因对matplotlib.use()的调用无效 因为已经选择了后端; matplotlib.use()必须在 pylab,matplotlib.pyplot之前调用, 或者第一次导入matplotlib.backends。
如果您需要使用特定的后端,请始终坚持此订单。
import matplotlib
matplotlib.use("TkAgg")
import matplotlib.pyplot as plt
plt.plot(..)
如果您不需要特定的后端(大多数情况下应该是这种情况),请让matplotlib决定选择哪一个 - omitt step 1&amp; 2!即可。特别是在共享代码时,您甚至无法确定所需后端的库是否安装在其他用户的系统上。