Can anyone help me to solve the problem which I have posted earlier
On Sat, Jun 29, 2019, 17:30 mallamma Reddy <[email protected]> wrote:
> I am trying to plot some random data using `pyqtgraph`. I used the
> "scrolling graph" example from "`pyqtgraph.examples`" as template to create
> a plot for my data.
>
> I have defined two functions here one for button event and other for
> update data.
> I want to plot the data when the checkbox and plot button is clicked.
> I found many more examples like this "Updating and PlotWidgets in
> PyQt4 for Live Plotting" but din't get idea how to use it for
> `pg.PlotWidget()`.
> The below code I found in "`pyqtgraph.examples`" and here they used
> `GraphicsWindow()`.
>
> def update():
> global data, curve, ptr
> data[:-1] = data[1:]
> data[-1] = np.random.normal()
> curve.setData(data)
> ptr += 1
> curve.setData(data)
> curve.setPos(ptr, 0)
> timer = pg.QtCore.QTimer()
> timer.timeout.connect(update)
> timer.start(50)
>
> But in my code i am using `self.graphicsView =
> pg.PlotWidget(self.centralwidget)`. Here the graph is static and its not
> rolling.So here i want to update the data but here data is not updating and
> plot is not moving .
> Can anyone help how can i update the data an how to use `plotWidget` and
> `QTimer`?
>
> class Ui_MainWindow(QtGui.QWidget):
> def __init__(self, parent=None):
> super(Ui_MainWindow, self).__init__(parent)
> # Desired Frequency (Hz) = 1 / self.FREQUENCY
> # USE FOR TIME.SLEEP (s)
> self.FREQUENCY = .004
>
> # Frequency to update plot (ms)
> # USE FOR TIMER.TIMER (ms)
> self.TIMER_FREQUENCY = self.FREQUENCY * 1000
>
> # Set X Axis range. If desired is [-10,0] then set LEFT_X =
> -10 and RIGHT_X = 0
> self.LEFT_X = 0
> self.RIGHT_X = 3
> self.X_Axis = np.arange(self.LEFT_X, self.RIGHT_X,
> self.FREQUENCY)
> self.buffer = int((abs(self.LEFT_X) +
> abs(self.RIGHT_X))/self.FREQUENCY)
> self.data = []
> self.graphicsView = pg.PlotWidget(self.centralwidget)
> ## self.graphicsView = pg.PlotWidget()
>
> self.graphicsView.plotItem.setMouseEnabled(x=False, y=False)
> self.graphicsView.setXRange(self.LEFT_X, self.RIGHT_X)
> self.graphicsView.setTitle('Scrolling Plot Example')
> self.graphicsView.setLabel('left', 'Value')
> self.graphicsView.setLabel('bottom', 'Time (s)')
>
> self.scrolling_plot = self.graphicsView.plot()
> ## self.scrolling_plot.setPen(197,235,255)
>
> self.layout = QtGui.QGridLayout()
> self.layout.addWidget(self.graphicsView)
>
> self.read_position_thread()
> self.start()
>
> def start(self):
> self.position_update_timer = QtCore.QTimer()
> self.position_update_timer.timeout.connect(self.Plot_Button)
>
> self.position_update_timer.start(self.get_scrolling_plot_timer_frequency())
>
> def read_position_thread(self):
> self.current_position_value = 0
> self.old_current_position_value = 0
> self.position_update_thread =
> Thread(target=self.read_position, args=())
> self.position_update_thread.daemon = True
> self.position_update_thread.start()
>
> def read_position(self):
> frequency = self.get_scrolling_plot_frequency()
> while True:
> try:
> self.current_position_value = random.randint(1,101)
> self.old_current_position_value =
> self.current_position_value
> time.sleep(frequency)
> except:
> self.current_position_value =
> self.old_current_position_value
>
> def Plot_Button(self):
> if self.Sensor1.isChecked():
> self.dataPoint = float(self.current_position_value)
> if len(self.data) >= self.buffer:
> del self.data[:1]
> self.data.append(self.dataPoint)
> self.scrolling_plot.setData(self.X_Axis[len(self.X_Axis) -
> len(self.data):], self.data)
> def clear_scrolling_plot(self):
> self.data[:] = []
>
> def get_scrolling_plot_frequency(self):
> return self.FREQUENCY
>
> def get_scrolling_plot_timer_frequency(self):
> return self.TIMER_FREQUENCY
>
> def get_scrolling_plot_layout(self):
> return self.layout
>
> def get_current_position_value(self):
> return self.current_position_value
>
> def get_scrolling_plot_widget(self):
> return self.graphicsView
>
> def Pause_Button(self):
> QCoreApplication.instance().quit()
> print("fail")
> def setupUi(self, MainWindow):
> MainWindow.setObjectName("MainWindow")
> MainWindow.resize(795, 359)
> self.centralwidget = QtWidgets.QWidget(MainWindow)
> self.centralwidget.setObjectName("centralwidget")
> self.Plot = QtWidgets.QPushButton(self.centralwidget)
> self.Plot.setGeometry(QtCore.QRect(530, 260, 80, 23))
> self.Plot.setObjectName("Plot")
> self.Plot.clicked.connect(self.Plot_Button)
> self.Pause = QtWidgets.QPushButton(self.centralwidget)
> self.Pause.setGeometry(QtCore.QRect(620, 260, 80, 23))
> self.Pause.setObjectName("Pause")#
> self.Pause.clicked.connect(self.Pause_Button)
> self.frame = QtWidgets.QFrame(self.centralwidget)
> self.frame.setGeometry(QtCore.QRect(520, 10, 251, 291))
> self.frame.setFrameShape(QtWidgets.QFrame.StyledPanel)
> self.frame.setFrameShadow(QtWidgets.QFrame.Raised)
> self.frame.setObjectName("frame")
> self.label = QtWidgets.QLabel(self.frame)
> self.label.setGeometry(QtCore.QRect(140, 6, 101, 20))
> ## self.label.setObjectName("Sampling Frequency")
> self.label.setObjectName("label")
> self.label_2 = QtWidgets.QLabel(self.frame)
> self.label_2.setGeometry(QtCore.QRect(150, 40, 51, 20))
> ## self.label_2.setObjectName("Offset")
> self.label_2.setObjectName("label_2")
> self.Sensor1 = QtWidgets.QCheckBox(self.frame)
> self.Sensor1.setGeometry(QtCore.QRect(30, 70, 74, 21))
> self.Sensor1.setObjectName("Sensor1")
> self.Sensor2 = QtWidgets.QCheckBox(self.frame)
> self.Sensor2.setGeometry(QtCore.QRect(30, 90, 74, 21))
> self.Sensor2.setObjectName("Sensor2")
> self.Sensor3 = QtWidgets.QCheckBox(self.frame)
> self.Sensor3.setGeometry(QtCore.QRect(30, 110, 74, 21))
> self.Sensor3.setObjectName("Sensor3")
> self.Sensor4 = QtWidgets.QCheckBox(self.frame)
> self.Sensor4.setGeometry(QtCore.QRect(30, 130, 71, 21))
> self.Sensor4.setObjectName("Sensor4")
> self.Sensor5 = QtWidgets.QCheckBox(self.frame)
> self.Sensor5.setGeometry(QtCore.QRect(30, 150, 74, 21))
> self.Sensor5.setObjectName("Sensor5")
> self.Sensor6 = QtWidgets.QCheckBox(self.frame)
> self.Sensor6.setGeometry(QtCore.QRect(30, 170, 74, 21))
> self.Sensor6.setObjectName("Sensor6")
> self.Sensor7 = QtWidgets.QCheckBox(self.frame)
> self.Sensor7.setGeometry(QtCore.QRect(30, 190, 74, 21))
> self.Sensor7.setObjectName("Sensor7")
> self.Sensor8 = QtWidgets.QCheckBox(self.frame)
> self.Sensor8.setGeometry(QtCore.QRect(30, 210, 71, 21))
> self.Sensor8.setObjectName("Sensor8")
> self.lineEdit_1 = QtWidgets.QLineEdit(self.frame)
> self.lineEdit_1.setGeometry(QtCore.QRect(20, 10, 113, 21))
> self.lineEdit_1.setObjectName("lineEdit_1")
> self.lineEdit_2 = QtWidgets.QLineEdit(self.frame)
> self.lineEdit_2.setGeometry(QtCore.QRect(20, 40, 113, 20))
> self.lineEdit_2.setObjectName("lineEdit_2")
> ## self.graphicsView =
> QtWidgets.QGraphicsView(self.centralwidget)
> self.graphicsView = pg.PlotWidget(self.centralwidget)
> ## self.graphicsView.setXRange(self.LEFT_X, self.RIGHT_X)
> self.graphicsView.setLabel('bottom', 'Time (s)')
> self.graphicsView.setLabel('left', 'Value')
> self.graphicsView.setGeometry(QtCore.QRect(5, 11, 501, 291))
> self.graphicsView.setObjectName("graphicsView")
> self.frame.raise_()
> self.Plot.raise_()
> self.Pause.raise_()
> self.graphicsView.raise_()
> MainWindow.setCentralWidget(self.centralwidget)
> self.menubar = QtWidgets.QMenuBar(MainWindow)
> self.menubar.setGeometry(QtCore.QRect(0, 0, 795, 21))
> self.menubar.setObjectName("menubar")
> self.menuFile = QtWidgets.QMenu(self.menubar)
> self.menuFile.setObjectName("menuFile")
> self.menuAbout = QtWidgets.QMenu(self.menubar)
> self.menuAbout.setObjectName("menuAbout")
> MainWindow.setMenuBar(self.menubar)
> self.statusbar = QtWidgets.QStatusBar(MainWindow)
> self.statusbar.setObjectName("statusbar")
> MainWindow.setStatusBar(self.statusbar)
> self.actionOpen = QtWidgets.QAction(MainWindow)
> self.actionOpen.setObjectName("actionOpen")
> self.actionSave_As = QtWidgets.QAction(MainWindow)
> self.actionSave_As.setObjectName("actionSave_As")
> self.actionScreenshot = QtWidgets.QAction(MainWindow)
> self.actionScreenshot.setObjectName("actionScreenshot")
> self.actionExit = QtWidgets.QAction(MainWindow)
> self.actionExit.setObjectName("actionExit")
> self.menuFile.addAction(self.actionOpen)
> self.menuFile.addAction(self.actionSave_As)
> self.menuFile.addAction(self.actionScreenshot)
> self.menuFile.addAction(self.actionExit)
> self.menubar.addAction(self.menuFile.menuAction())
> self.menubar.addAction(self.menuAbout.menuAction())
> self.retranslateUi(MainWindow)
> QtCore.QMetaObject.connectSlotsByName(MainWindow)
>
> def retranslateUi(self, MainWindow):
> _translate = QtCore.QCoreApplication.translate
> MainWindow.setWindowTitle(_translate("MainWindow",
> "MainWindow"))
> self.Plot.setText(_translate("MainWindow", "Plot"))
> self.Pause.setText(_translate("MainWindow", "Pause"))
> self.label.setText(_translate("MainWindow", "Sampling
> Frequency"))
> self.label_2.setText(_translate("MainWindow", "Offset"))
> self.Sensor1.setText(_translate("MainWindow", "Sensor1"))
> self.Sensor2.setText(_translate("MainWindow", "Sensor2"))
> self.Sensor3.setText(_translate("MainWindow", "Sensor3"))
> self.Sensor4.setText(_translate("MainWindow", "Sensor4"))
> self.Sensor5.setText(_translate("MainWindow", "Sensor5"))
> self.Sensor6.setText(_translate("MainWindow", "Sensor6"))
> self.Sensor7.setText(_translate("MainWindow", "Sensor7"))
> self.Sensor8.setText(_translate("MainWindow", "Sensor8"))
> self.menuFile.setTitle(_translate("MainWindow", "File"))
> self.menuAbout.setTitle(_translate("MainWindow", "About"))
> self.actionOpen.setText(_translate("MainWindow", "Open"))
> self.actionSave_As.setText(_translate("MainWindow", "Save As"))
> self.actionScreenshot.setText(_translate("MainWindow",
> "Screenshot"))
> self.actionExit.setText(_translate("MainWindow", "Exit"))
>
>
> if __name__ == "__main__":
> import sys
> app = QtWidgets.QApplication(sys.argv)
> MainWindow = QtWidgets.QMainWindow()
> ui = Ui_MainWindow()
> ui.setupUi(MainWindow)
> MainWindow.show()
> sys.exit(app.exec_())
> if (sys.flags.interactive != 1) or not hasattr(QtCore,
> 'PYQT_VERSION'):
> QtGui.QApplication.instance().exec_()
>
> Now I made changes in my code. Values are updating but I am not able
> to see the graph on widget since I am using `self.graphicsView =
> pg.PlotWidget(self.centralwidget)` inside the function .
> In the above example they using `self.scrolling_plot_widget =
> pg.PlotWidget()`.
> How do I change it for plotting in `pg.PlotWidget(self.centralwidget)`.
> where I am missing the code and where and in which funcion i need to call
> the PlotWidget()????.
>
>
>
> On Monday, 30 June 2014 00:13:40 UTC+5:30, Federico BolaƱos wrote:
>>
>>
>> Hello everyone!
>>
>> I am wondering if it's possible to develop a script that uses pyqtgraph
>> and mimics the performance of this script:
>> https://www.youtube.com/watch?v=HsV-LK3KO1U#t=77
>> source code:
>> http://www.swharden.com/blog/2012-06-14-multichannel-usb-analog-sensor-with-atmega48/
>>
>> If so, could anyone point me to an example that does this? or help me
>> write part of the code?
>>
>> The reason I don't want to use that particular script is because I think
>> pyqtgraph will offer much more flexibility, I can resize the plot, perhaps
>> have another plot that does an FFT of the signal, or even do real time
>> signal processing which may be harder to implement on that example script.
>>
>> In short, I want to be able to plot the signal coming from an ADC as fast
>> as I can. I wrote a class that allows me to interface with said ADC and I
>> would like to obtain the data as fast as I can and at the same time plot
>> the data a correct time x axis. The ADC class that I wrote returns an
>> integer each time it is polled, sort of like if I were to do
>> random.randint(0, 1024).
>>
>> I have been looking for an example that uses this library since it is
>> very lightweight and runs nicely on a raspberry pi! (So does the above
>> script, but I would really like the flexibility that pyqtgraph provides)
>>
>>
>> Thanks for reading,
>> Federico
>>
> --
> You received this message because you are subscribed to the Google Groups
> "pyqtgraph" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/pyqtgraph/60b3d181-d232-40a0-99cd-bd8f3cca4907%40googlegroups.com
> <https://groups.google.com/d/msgid/pyqtgraph/60b3d181-d232-40a0-99cd-bd8f3cca4907%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
> For more options, visit https://groups.google.com/d/optout.
>
--
You received this message because you are subscribed to the Google Groups
"pyqtgraph" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/pyqtgraph/CAACFV6Kgfdca58uhMfVJd6QY84bevqMGzRFdz_wRs3vRO2HrGA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.