在PyQt中嵌入Matplotlib

时间:2012-06-29 17:09:50

标签: python matplotlib pyqt backend

我正在尝试测试Matplotlib是否在Ubuntu上的PyQt中工作。我一直在使用PyQt,我想在Pyqt中嵌入Matplotlib。我按照http://eli.thegreenplace.net/2009/01/20/matplotlib-with-pyqt-guis/给出的代码进行了操作,但在导入matplotlib时会产生一些错误。

from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
  File "/usr/lib/pymodules/python2.6/matplotlib/backends/backend_qt4agg.py", line 9, in <module>
    from matplotlib.figure import Figure
  File "/usr/lib/pymodules/python2.6/matplotlib/figure.py", line 18, in <module>
    from axes import Axes, SubplotBase, subplot_class_factory
  File "/usr/lib/pymodules/python2.6/matplotlib/axes.py", line 2, in <module>
    import math, sys, warnings, datetime, new
  File "/home/kasa/Desktop/new.py", line 25, in <module>
    from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
ImportError: cannot import name FigureCanvasQTAgg

我从终端运行这些导入命令,它工作正常。 有人可以弄清楚我的安装有什么问题。

1 个答案:

答案 0 :(得分:3)

阅读追溯。

您尝试从FigureCanvasQTAgg导入backend_qt4agg

from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas

它尝试从Figure导入figure

  File "/usr/lib/pymodules/python2.6/matplotlib/backends/backend_qt4agg.py", line 9, in <module>
    from matplotlib.figure import Figure

试图从axes导入几件事:

  File "/usr/lib/pymodules/python2.6/matplotlib/figure.py", line 18, in <module>
    from axes import Axes, SubplotBase, subplot_class_factory

它也导入了几个模块。注意最后一个,new

  File "/usr/lib/pymodules/python2.6/matplotlib/axes.py", line 2, in <module>
    import math, sys, warnings, datetime, new

它在哪里寻找它?而不是内置模块,它转到

  File "/home/kasa/Desktop/new.py", line 25, in <module>

,我想这是你的档案,它又回来了:

    from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas

Python意识到它无法导入FigureCanvasQTAgg,因为它发现自己处于循环导入地狱,因此错误:

ImportError: cannot import name FigureCanvasQTAgg

长话短说

您的文件会屏蔽内置new module。解决方案很简单:重命名文件(并从文件夹中删除new.pyc)。