gpt4 book ai didi

python - matplotlib 图从 QT 抛出 TypeError

转载 作者:太空宇宙 更新时间:2023-11-03 15:32:51 30 4
gpt4 key购买 nike

问题

当我运行时

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

我需要做什么才能解决这个问题?

更多信息

导入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.

最佳答案

我猜测发生了类似以下的事情,尽管我不能确定,因为没有给出有关两种情况的确切代码。

当您(无意中)尝试使用未安装的后端时,可能会出现第一个错误。

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.

这就是你的目标

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.

解决方案:

如果您需要使用特定后端,请始终遵循此顺序。

  1. 导入matplotlib
  2. matplotlib.use("TkAgg")
  3. 将 matplotlib.pyplot 导入为 plt
  4. 绘图,例如plt.plot(..)

如果您不需要特定的后端(大多数情况下都是这种情况),请让 matplotlib 决定选择哪一个 - 省略步骤 1 和 2!。特别是在共享代码时,您甚至无法确定所需后端的库是否安装在其他用户的系统上。

关于python - matplotlib 图从 QT 抛出 TypeError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42765991/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com