Python matlplotlib figure从QT抛出TypeError 问题
当我跑的时候Python matlplotlib figure从QT抛出TypeError 问题,python,qt,matplotlib,Python,Qt,Matplotlib,当我跑的时候 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 =
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'
我要怎么做才能解决这个问题
更多信息
当导入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']
我猜发生了类似下面的事情,尽管我不能确定,因为没有给出这两个案例的确切代码 第一个错误可能是在您(不自由地)尝试使用未安装的后端时出现的
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()的调用无效
因为已经选择了后端;
必须在pylab、matplotlib.pyplot、,
或matplotlib.backends首次导入
解决方案:
如果需要使用特定的后端,请始终遵循此顺序
import matplotlib
matplotlib.use(“TkAgg”)
将matplotlib.pyplot导入为plt
plt.绘图(..)
如果您不需要特定的后端(大多数情况下都是这样),让matplotlib决定选择哪个后端-忽略步骤1和2。尤其是在共享代码时,您甚至无法确保所需后端的库已安装在其他用户的系统上 因为我不确定为什么错误出现在qt4中,而您有qt5:可能是您在另一个同样安装的python版本中不间断地运行这个错误吗?您还可以尝试使用
QT5Agg
backend,import matplotlib;matplotlib.use(“QT5Agg”)
@ImportanceOfBeingErnest:现在我可以运行plt.figure
,但我得到了问题中添加的消息。我觉得这可能不健康。。。怎么说?现在是什么意思?你在哪个环境下运行这个?它是一个新启动的python内核,还是会有一些旧的导入挂起?@ImportanceOfBeingErnest:“now”=在我按照你的要求做了之后。之前,我没有使用import matplotlib
加载matplotlib。这是一个新启动的内核。所以,基本上你是说matplotlib.use(“QT5Agg”)
被完全忽略了?在控制台$>python script.py
中运行脚本时是否存在问题?
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.