python - How to use matplotlib with PyQt4 -


i want plot figure embedded matplotlib in pyqt. using qt designer main window, , writing python code signal , slots connexion part.

so code looks :

import sys pyqt4 import qtcore, qtgui, uic matplotlib.backends.backend_qt4agg import figurecanvasqtagg figurecanvas  matplotlib.backends.backend_qt4agg import navigationtoolbar2qt navigationtoolbar import matplotlib.pyplot plt import pandas pd import numpy np   qtcreatorfile = "main.ui" # qt designer file   ui_mainwindow, qtbaseclass = uic.loaduitype(qtcreatorfile)  class myapp(qtgui.qmainwindow, ui_mainwindow):     def __init__(self):         qtgui.qmainwindow.__init__(self)         ui_mainwindow.__init__(self)         self.setupui(self)          self.figure = plt.figure()          self.canvas = figurecanvas(self.figure)          self.csvbutton.clicked.connect(self.plot)      def plot(self):          filepath="/path csv file here"         df= pd.read_csv(str(filepath),index_col='date')         df.index = pd.to_datetime(df.index, unit='s')         ax = self.figure.add_subplot(111)         ax.hold(false)         ax.plot(df, '*-')         self.canvas.draw()    if __name__ == "__main__":     app = qtgui.qapplication(sys.argv)     window = myapp()     window.show()     sys.exit(app.exec_()) 

my main problem connexion between qt designer file , python code, couldn't set canvas widget directly in qt designer , i'm still struggling find error lays in code. appreciated, thank you.

in order use matplotlib in qt designer can not done directly, must promote qwidget use figurecanvas or better class inherits show below, first create class called canvas in file called canvas.py:

canvas.py

from matplotlib.backends.backend_qt4agg import figurecanvasqtagg figurecanvas  import matplotlib.pyplot plt  class canvas(figurecanvas):     def __init__(self, parent=none):         self.figure = plt.figure()         figurecanvas.__init__(self, self.figure)         self.setparent(parent) 

after creating design through qt designer, have elements want, want place argument use widget element in containers, , name canvas:

enter image description here

then promote right click , choose option promoted ...:

enter image description here

obtaining shown in following image, in promoted class name place canvas name of class, , in header file place canvas.h (in header file file.py file placed, example package.subpackage.file.h), press add , after promote:

enter image description here

at end file structure similar following:

. ├── canvas.py └── main.ui 

then create file main.py place code small variations:

main.py

import matplotlib matplotlib.use('qt4agg')  import sys pyqt4 import qtcore, qtgui, uic import matplotlib.pyplot plt import pandas pd import numpy np   qtcreatorfile = "main.ui" # qt designer file   ui_mainwindow, qtbaseclass = uic.loaduitype(qtcreatorfile)  class myapp(qtgui.qmainwindow, ui_mainwindow):     def __init__(self):         qtgui.qmainwindow.__init__(self)         ui_mainwindow.__init__(self)         self.setupui(self)         self.csvbutton.clicked.connect(self.plot)      def plot(self):          filepath="data.csv"         df= pd.read_csv(str(filepath),index_col='date')         ax = self.canvas.figure.add_subplot(111)         ax.hold(false)         ax.plot(df, '*-')         self.canvas.draw()    if __name__ == "__main__":     app = qtgui.qapplication(sys.argv)     window = myapp()     window.show()     sys.exit(app.exec_()) 

in end following:

enter image description here

you can find complete project here


Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

Add new key value to json node in java -