[PyQt] problem with figure in pyqt

Hans-Peter Jansen hpj at urpla.net
Sun Mar 27 21:33:51 BST 2011


On Sunday 27 March 2011, 20:46:51 nimi pillai wrote:
> Hi,
>
> I want an application that displays a plot when a 'plot ' button is
> clicked. My code is given below. The figure window doesnot appear
> when i run the code. But if there is any error as i hav commented
> '#plt' the figure window appears. Can someone please tell me why this
> happens?
>
>
> from PyQt4 import QtGui
> from PyQt4 import QtCore
> from matplotlib.backends.backend_qt4agg \
>   import FigureCanvasQTAgg as FigureCanvas
> from matplotlib.backends.backend_qt4agg \
>   import NavigationToolbar2QTAgg as NavigationToolbar
> from matplotlib.backends.backend_qt4agg \
>            import FigureCanvasQTAgg as FigureCanvas
> from matplotlib.figure import Figure
> import numpy as n
> import sys
> import os
>
> # that contains 2 pushbuttons ; 1st for plt ,2nd for close
> class Ui_MainWindow(object):
>     def setupUi(self, MainWindow):
>         MainWindow.setObjectName("MainWindow")
>         MainWindow.resize(800, 600)
>         self.centralwidget = QtGui.QWidget(MainWindow)
>         self.centralwidget.setObjectName("centralwidget")
>         self.pushButton = QtGui.QPushButton(self.centralwidget)
>         self.pushButton.setGeometry(QtCore.QRect(120, 170, 75, 23))
>         self.pushButton.setObjectName("pushButton")
>         self.pushButton_2 = QtGui.QPushButton(self.centralwidget)
>         self.pushButton_2.setGeometry(QtCore.QRect(270, 170, 75, 23))
>         self.pushButton_2.setObjectName("pushButton_2")
>         MainWindow.setCentralWidget(self.centralwidget)
>         self.statusbar = QtGui.QStatusBar(MainWindow)
>         self.statusbar.setObjectName("statusbar")
>         MainWindow.setStatusBar(self.statusbar)
>
>         self.retranslateUi(MainWindow)
>         QtCore.QObject.connect(self.pushButton_2,
> QtCore.SIGNAL("clicked()"), MainWindow.close)
>         QtCore.QMetaObject.connectSlotsByName(MainWindow)
>
>     def retranslateUi(self, MainWindow):
>        
> MainWindow.setWindowTitle(QtGui.QApplication.translate("MainWindow",
> "MainWindow", None, QtGui.QApplication.UnicodeUTF8))
>        
> self.pushButton.setText(QtGui.QApplication.translate("MainWindow",
> "Plot", None, QtGui.QApplication.UnicodeUTF8))
>        
> self.pushButton_2.setText(QtGui.QApplication.translate("MainWindow",
> "close", None, QtGui.QApplication.UnicodeUTF8))
>
>
> #canvas widget
> class MplCanvas(FigureCanvas):
>        def __init__(self,parent=None):
>            self.fig = Figure()
>            self.ax = self.fig.add_subplot(111)
>            FigureCanvas.__init__(self, self.fig)
>            self.fig.clf()
>
>
> class MplWidget(QtGui.QWidget):
>
>        def __init__(self, parent = None):
>            QtGui.QWidget.__init__(self, parent)
>            self.canvas = MplCanvas()
>            self.ntb = NavigationToolbar(self.canvas, self.canvas)
>            self.v = QtGui.QVBoxLayout()
>            self.v.addWidget(self.canvas)
>            self.v.addWidget(self.ntb)
>            self.ax=self.canvas.ax
>            self.setLayout(self.v)
>
> class MyForm(QtGui.QMainWindow):
>
>   def __init__(self, parent=None):
>     QtGui.QWidget.__init__(self, parent)
>     self.ui = Ui_MainWindow()
>     self.ui.setupUi(self)
>     QtCore.QObject.connect(self.ui.pushButton,
> QtCore.SIGNAL("clicked()"), self.plot)
>
>   def plot(self):
>      w=MplWidget()

You need to keep a reference to your MplWidget here, or give it a 
parent.

>      x=n.arange(0,20)
>      y=n.sin(x)
>      print x
>      print y
>      y1=n.cos(x)
>      w.ax.plot(x,y)
>      #w.canvas.ax.plot(x,y1)
>      w.show()
>      plt
>
>
> if __name__ == "__main__":
>   app = QtGui.QApplication(sys.argv)
>   myapp = MyForm()
>   myapp.show()
>   sys.exit(app.exec_())

Pete


More information about the PyQt mailing list