PDA

View Full Version : PyQT and Qwt read data and plot -- not able to plot



diedro
1st August 2014, 16:31
Dear all,
I am new in this forum and I am also new in the PyQT-QT-Qwt environment.
I am trying to write a program in PyQT and Qwt that should read the data from a file ant then plot it.
Unfortunately the pram does not work and I don't know why.

This is my code:
There is a button (pushButton_1) that open a folder where I can select a file.
The button pushButton_1 calls the def showDialog(self): action that open the folder and then plot the data. This is the point, I am not able to plot the data.

This is my full code. It is no so long, so I post the entire code. I hope that is OK for the administrator.




import numpy as np
from PyQt4 import QtCore, QtGui
import PyQt4.Qwt5 as Qwt
import sys, os, csv
from PyQt4.QtCore import *
from PyQt4.QtGui import *
import random


try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
def _fromUtf8(s):
return s

try:
_encoding = QtGui.QApplication.UnicodeUTF8
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig)

class Ui_MainWindow(QtGui.QMainWindow):
def setupUi(self, MainWindow):
MainWindow.setObjectName(_fromUtf8("MainWindow"))
MainWindow.resize(800, 663)
self.centralwidget = QtGui.QWidget(MainWindow)
self.centralwidget.setObjectName(_fromUtf8("centralwidget"))



self.pushButton_1 = QtGui.QPushButton(self.centralwidget)
self.pushButton_1.setGeometry(QtCore.QRect(60, 20, 311, 27))
self.pushButton_1.setObjectName(_fromUtf8("pushButton_1"))
self.pushButton_2 = QtGui.QPushButton(self.centralwidget)
self.pushButton_2.setGeometry(QtCore.QRect(60, 70, 311, 27))
self.pushButton_2.setObjectName(_fromUtf8("pushButton_2"))



self.qwtPlot_2 = Qwt.QwtPlot(self.centralwidget)
self.qwtPlot_2.setGeometry(QtCore.QRect(60, 170, 400, 200))
self.qwtPlot_2.setObjectName(_fromUtf8("qwtPlot_2"))
self.qwtPlot_2.setAxisTitle(Qwt.QwtPlot.xBottom, 'x -->')
self.qwtPlot_2.setAxisTitle(Qwt.QwtPlot.yLeft, 'y -->')
grid = Qwt.QwtPlotGrid()
pen = QPen(Qt.DotLine)
pen.setColor(Qt.black)
pen.setWidth(0)
grid.setPen(pen)
grid.attach(self.qwtPlot_2)

self.qwtPlot_3 = Qwt.QwtPlot(self.centralwidget)
self.qwtPlot_3.setGeometry(QtCore.QRect(60, 390, 400, 200))
self.qwtPlot_3.setObjectName(_fromUtf8("qwtPlot_3"))
self.qwtPlot_3.setAxisTitle(Qwt.QwtPlot.xBottom, 'x -->')
self.qwtPlot_3.setAxisTitle(Qwt.QwtPlot.yLeft, 'y -->')
grid = Qwt.QwtPlotGrid()
pen = QPen(Qt.DotLine)
pen.setColor(Qt.black)
pen.setWidth(0)
grid.setPen(pen)
grid.attach(self.qwtPlot_3)

MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtGui.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 25))
self.menubar.setObjectName(_fromUtf8("menubar"))
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtGui.QStatusBar(MainWindow)
self.statusbar.setObjectName(_fromUtf8("statusbar"))
MainWindow.setStatusBar(self.statusbar)

self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)


self.centralwidget.connect(self.pushButton_1, QtCore.SIGNAL('clicked()'), self.showDialog)



def retranslateUi(self, MainWindow):
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow", None))
self.pushButton_1.setText(_translate("MainWindow", "Selec file txt", None))
self.pushButton_2.setText(_translate("MainWindow", "Plot", None))

#------------------------------------------------------------------------
#azione apertura file0
#------------------------------------------------------------------------
def showDialog(self):
fname = QtGui.QFileDialog.getOpenFileName(self, 'Open a data file', '.', 'txt files (*.txt);;All Files (*.*)')
f = open(fname, 'r')
with f:
data = np.genfromtxt(f, skiprows=1)
self.tdata = data[:,0]
self.cdata = data[:,1]
curve=Qwt.QwtPlotCurve('ciao')
curve.setData(range(len(self.tdata)), self.tdata)
curve.attach(self.qwtPlot_3)
print 'hello'
#
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
MainWindow = QtGui.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())


Thanks in advance to all

Dear all,
I have understood why:

I have missed the replot-one:


curve = Qwt.QwtPlotCurve('Q')
curve.attach(self.qwtPlot_2)
curve.setPen(Qt.QPen(Qt.Qt.blue, 2))
curve.setData(self.tdata, self.cdata)
self.qwtPlot_2.replot()


thanks to all and sorry for your time