I used the "scrolling graph" example from pyqtgraph.examples
as template to create a plot, which shows a continuous sinus wave. Now, I would like to use buttons to change the amplitude of the sinus wave. This is why I used another structure (see the code below). This doesn't work, it only plots a static sinus wave.
How can I use my update function to plot the continuous sinus wave? How can I chage the amplitude by using the buttons? I have already checked this and this without success.
import sys
from PyQt4 import QtGui, QtCore
import numpy as np
import pyqtgraph as pg
class sinus_wave(QtGui.QWidget):
def __init__(self):
super(sinus_wave, self).__init__()
self.initUI()
def initPlot(self, plots):
a = 10
ptr1 = 30
data1 = a*np.sin(np.linspace(0,30,121))
plots.plot(data1)
timer = pg.QtCore.QTimer()
timer.timeout.connect(lambda: self.update(self,p1 = plots,data1= data1, ptr1 = ptr1))
timer.start(50)
def initUI(self):
IncreaseButton = QtGui.QPushButton("Increase Amplitude")
DecreaseButton = QtGui.QPushButton("Decrease Amplitude")
p1 = pg.PlotWidget()
hbox = QtGui.QVBoxLayout()
hbox.addWidget(p1)
hbox.addWidget(IncreaseButton)
hbox.addWidget(DecreaseButton)
self.initPlot(p1)
self.setLayout(hbox)
self.setGeometry(10, 10, 1000, 600)
self.setWindowTitle('Sinuswave')
self.show()
def update(self, p1, data1, ptr1):
data1[:-1] = data1[1:]
data1[-1] = np.sin(ptr1/4)
p1.plot(data1)
ptr1 += 1
p1.show()
def IncreaseButtonClick(self):
print ("Amplitude increased")
def DecreaseButtonClick(self):
print ("Amplitude decreased")
def main():
app = QtGui.QApplication(sys.argv)
app.setApplicationName('Sinuswave')
ex = sinus_wave()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
So this is not really a pyqtgraph question. Mostly you need to read up on classes in python and on pyqt basics. But I will try to give you some quick fixes for your problems, and hopefully you will learn something on the way.
For making your buttons work you need to connect them to the methods. Try looking at the answer here: https://stackoverflow.com/a/8763339/4328381
You would need something like this
def qt_connections(self):
self.increasebutton.clicked.connect(self.on_increasebutton_clicked)
self.decreasebutton.clicked.connect(self.on_decreasebutton_clicked)
Then to make the buttons actually do something they need to change your amplitude. First store the amplitude as an attribute for the instance. Also store a t attribute to later make it move.
Your a and ptr1 are just variables inside the method that will be cleared up once the method finishes. By using self.
they become instance attributes that can be used from the other methods in the class.
def __init__(self):
...
self.amplitude = 10
self.t = 0
...
Then you can change the amplitude in the method connected to the button.
def on_increasebutton_clicked(self):
print ("Amplitude increased")
self.amplitude += 1
self.updateplot()
Then to make it continous you first need to make sure that the timer works. Try adding a print("test")
in it and you will see that it doesn't.You need to keep a reference of it, otherwise it will be cleaned up.
self.timer = pg.QtCore.QTimer()
self.timer.timeout.connect(self.moveplot)
self.timer.start(50)
To make the sine move continuously you need to move it in the method connected to the timer, you can simple create a moveplot method.
def moveplot(self):
self.t+=1
self.updateplot()
And then to put it together create and updateplot method which uses the attributes created earlier.
def updateplot(self):
print "Update"
data1 = self.amplitude*np.sin(np.linspace(0,30,121)+self.t)
self.plotcurve.setData(data1)
In the end you will get something like this
import sys
from PyQt4 import QtGui, QtCore
import numpy as np
import pyqtgraph as pg
class sinus_wave(QtGui.QWidget):
def __init__(self):
super(sinus_wave, self).__init__()
self.init_ui()
self.qt_connections()
self.plotcurve = pg.PlotCurveItem()
self.plotwidget.addItem(self.plotcurve)
self.amplitude = 10
self.t = 0
self.updateplot()
self.timer = pg.QtCore.QTimer()
self.timer.timeout.connect(self.moveplot)
self.timer.start(500)
def init_ui(self):
self.setWindowTitle('Sinuswave')
hbox = QtGui.QVBoxLayout()
self.setLayout(hbox)
self.plotwidget = pg.PlotWidget()
hbox.addWidget(self.plotwidget)
self.increasebutton = QtGui.QPushButton("Increase Amplitude")
self.decreasebutton = QtGui.QPushButton("Decrease Amplitude")
hbox.addWidget(self.increasebutton)
hbox.addWidget(self.decreasebutton)
self.setGeometry(10, 10, 1000, 600)
self.show()
def qt_connections(self):
self.increasebutton.clicked.connect(self.on_increasebutton_clicked)
self.decreasebutton.clicked.connect(self.on_decreasebutton_clicked)
def moveplot(self):
self.t+=1
self.updateplot()
def updateplot(self):
print "Update"
data1 = self.amplitude*np.sin(np.linspace(0,30,121)+self.t)
self.plotcurve.setData(data1)
def on_increasebutton_clicked(self):
print ("Amplitude increased")
self.amplitude += 1
self.updateplot()
def on_decreasebutton_clicked(self):
print ("Amplitude decreased")
self.amplitude -= 1
self.updateplot()
def main():
app = QtGui.QApplication(sys.argv)
app.setApplicationName('Sinuswave')
ex = sinus_wave()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
来源:https://stackoverflow.com/questions/35056635/how-to-update-a-realtime-plot-and-use-buttons-to-interact-in-pyqtgraph