简体   繁体   中英

Embedding Matplotlib in PyQt

I am trying to test if Matplotlib works in PyQt on Ubuntu. I have been working with PyQt and I want to embed Matplotlib in 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 .

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 :

from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas

It tried to import Figure from 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 :

  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 :

  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:

ImportError: cannot import name FigureCanvasQTAgg

Long story short

Your file masks the built-in new module . Solution is simple: Rename the file (and also remove the new.pyc from the folder).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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