简体   繁体   English

在PyQt中嵌入Matplotlib

[英]Embedding Matplotlib in PyQt

I am trying to test if Matplotlib works in PyQt on Ubuntu. 我正在尝试测试Matplotlib是否可以在Ubuntu的PyQt中工作。 I have been working with PyQt and I want to embed Matplotlib in Pyqt. 我一直在与PyQt合作,我想将Matplotlib嵌入Pyqt。 I followed the code given at http://eli.thegreenplace.net/2009/01/20/matplotlib-with-pyqt-guis/ but It generates some errors while importing the 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

I run these import commands from terminal and it works fine. 我从终端运行这些导入命令,它工作正常。 Can someone figure out whats wrong with my installation. 有人可以找出我的安装有什么问题吗?

Read the traceback. 阅读回溯。

You tried to import FigureCanvasQTAgg from backend_qt4agg : 您尝试汇入FigureCanvasQTAggbackend_qt4agg

from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas

It tried to import Figure from figure : 它试图从figure导入Figure

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

which tried to import several things from axes : 试图从axes导入一些东西:

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

It imports several modules as well. 它还导入了几个模块。 Notice the last one, new : 注意最后一个, new

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

and where does it look for it? 它在哪里寻找? Instead of the built-in module, it goes to 代替内置模块,它转到

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

which, I suppose is your file and it goes back again: 我想这是您的文件,它又返回了:

    from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas

Python realizes that it can't import FigureCanvasQTAgg because it finds itself in a circular import hell, thus the error: Python意识到无法导入FigureCanvasQTAgg因为它发现自己处于循环导入地狱,因此出现错误:

ImportError: cannot import name FigureCanvasQTAgg

Long story short 长话短说

Your file masks the built-in new module . 您的文件将掩盖内置的new模块 Solution is simple: Rename the file (and also remove the new.pyc from the folder). 解决方案很简单:重命名文件(并从文件夹中删除new.pyc )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM