Hi,<br><br>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?<br>
<br><br>from PyQt4 import QtGui<br>from PyQt4 import QtCore<br>from matplotlib.backends.backend_qt4agg \<br> import FigureCanvasQTAgg as FigureCanvas<br>from matplotlib.backends.backend_qt4agg \<br> import NavigationToolbar2QTAgg as NavigationToolbar<br>
from matplotlib.backends.backend_qt4agg \<br> import FigureCanvasQTAgg as FigureCanvas<br>from matplotlib.figure import Figure<br>import numpy as n<br>import sys<br>import os<br><br># that contains 2 pushbuttons ; 1st for plt ,2nd for close<br>
class Ui_MainWindow(object):<br> def setupUi(self, MainWindow):<br> MainWindow.setObjectName("MainWindow")<br> MainWindow.resize(800, 600)<br> self.centralwidget = QtGui.QWidget(MainWindow)<br>
self.centralwidget.setObjectName("centralwidget")<br> self.pushButton = QtGui.QPushButton(self.centralwidget)<br> self.pushButton.setGeometry(QtCore.QRect(120, 170, 75, 23))<br> self.pushButton.setObjectName("pushButton")<br>
self.pushButton_2 = QtGui.QPushButton(self.centralwidget)<br> self.pushButton_2.setGeometry(QtCore.QRect(270, 170, 75, 23))<br> self.pushButton_2.setObjectName("pushButton_2")<br> MainWindow.setCentralWidget(self.centralwidget)<br>
self.statusbar = QtGui.QStatusBar(MainWindow)<br> self.statusbar.setObjectName("statusbar")<br> MainWindow.setStatusBar(self.statusbar)<br><br> self.retranslateUi(MainWindow)<br> QtCore.QObject.connect(self.pushButton_2, QtCore.SIGNAL("clicked()"), MainWindow.close)<br>
QtCore.QMetaObject.connectSlotsByName(MainWindow)<br><br> def retranslateUi(self, MainWindow):<br> MainWindow.setWindowTitle(QtGui.QApplication.translate("MainWindow", "MainWindow", None, QtGui.QApplication.UnicodeUTF8))<br>
self.pushButton.setText(QtGui.QApplication.translate("MainWindow", "Plot", None, QtGui.QApplication.UnicodeUTF8))<br> self.pushButton_2.setText(QtGui.QApplication.translate("MainWindow", "close", None, QtGui.QApplication.UnicodeUTF8))<br>
<br><br>#canvas widget<br>class MplCanvas(FigureCanvas):<br> def __init__(self,parent=None):<br> self.fig = Figure()<br> <a href="http://self.ax">self.ax</a> = self.fig.add_subplot(111)<br> FigureCanvas.__init__(self, self.fig)<br>
self.fig.clf()<br><br><br>class MplWidget(QtGui.QWidget):<br><br> def __init__(self, parent = None):<br> QtGui.QWidget.__init__(self, parent)<br> self.canvas = MplCanvas()<br> self.ntb = NavigationToolbar(self.canvas, self.canvas)<br>
self.v = QtGui.QVBoxLayout()<br> self.v.addWidget(self.canvas)<br> self.v.addWidget(self.ntb)<br> <a href="http://self.ax">self.ax</a>=<a href="http://self.canvas.ax">self.canvas.ax</a><br>
self.setLayout(self.v)<br><br>class MyForm(QtGui.QMainWindow):<br> <br> def __init__(self, parent=None):<br> QtGui.QWidget.__init__(self, parent)<br> self.ui = Ui_MainWindow()<br> self.ui.setupUi(self) <br>
QtCore.QObject.connect(self.ui.pushButton, QtCore.SIGNAL("clicked()"), self.plot)<br><br> def plot(self):<br> w=MplWidget()<br> x=n.arange(0,20)<br> y=n.sin(x)<br> print x<br> print y<br>
y1=n.cos(x)<br> w.ax.plot(x,y)<br> #w.canvas.ax.plot(x,y1)<br> w.show()<br> plt<br><br><br>if __name__ == "__main__":<br> app = QtGui.QApplication(sys.argv)<br> myapp = MyForm()<br> myapp.show()<br>
sys.exit(app.exec_())<br><br>