如何更新实时绘图并使用按钮在 pyqtgraph 中进行交互?

How to update a realtime plot and use buttons to interact in pyqtgraph?

我使用 pyqtgraph.examples 中的 "scrolling graph" 示例作为模板来创建绘图,显示连续的正弦波。现在,我想用按钮来改变正弦波的幅度。这就是我使用另一种结构的原因(参见下面的代码)。这是行不通的,它只绘制一个静态的正弦波。 如何使用我的更新功能绘制连续的正弦波?如何使用按钮改变振幅?我已经检查过 this and 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.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()

所以这不是真正的 pyqtgraph 问题。大多数情况下,您需要阅读 python 中的 classes 和 pyqt 基础知识。但我会尝试为您提供一些快速解决问题的方法,希望您能在途中学到一些东西。

为了使您的按钮工作,您需要将它们连接到方法。尝试在此处查看答案:

你需要这样的东西

def qt_connections(self):
    self.increasebutton.clicked.connect(self.on_increasebutton_clicked)
    self.decreasebutton.clicked.connect(self.on_decreasebutton_clicked)

然后让按钮真正做一些他们需要改变你的振幅的事情。首先将振幅存储为实例的属性。还存储一个 t 属性以供以后移动。

你的 a 和 ptr1 只是方法中的变量,一旦方法完成就会被清除。通过使用 self.,它们成为可以从 class.

中的其他方法使用的实例属性
def __init__(self):
    ...
    self.amplitude = 10
    self.t = 0
    ...

然后你可以在连接到按钮的方法中改变振幅。

def on_increasebutton_clicked(self):
    print ("Amplitude increased")
    self.amplitude += 1
    self.updateplot()

然后要使其连续,您首先需要确保计时器正常工作。尝试在其中添加一个 print("test"),你会发现它不需要 t.You 保留它的引用,否则它将被清理。

    self.timer = pg.QtCore.QTimer()
    self.timer.timeout.connect(self.moveplot)
    self.timer.start(50)

要使正弦连续移动,您需要在连接到计时器的方法中移动它,您可以简单地创建一个 moveplot 方法。

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)

最后你会得到这样的东西

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()