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

C W*_*ler 1 python pyqt4 pyqtgraph

我使用“滚动图”示例作为pyqtgraph.examples模板来创建一个图,该图显示了连续的正弦波。现在,我想使用按钮来改变正弦波的幅度。这就是我使用另一种结构的原因(参见下面的代码)。这不起作用,它只绘制静态正弦波。如何使用更新函数绘制连续正弦波?如何使用按钮更改幅度?我已经检查过这个这个但没有成功。

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()
Run Code Online (Sandbox Code Playgroud)

lud*_*dek 5

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

为了使按钮正常工作,您需要将它们连接到方法。尝试在这里查看答案:/sf/answers/613433761/

你需要这样的东西

def qt_connections(self):
    self.increasebutton.clicked.connect(self.on_increasebutton_clicked)
    self.decreasebutton.clicked.connect(self.on_decreasebutton_clicked)
Run Code Online (Sandbox Code Playgroud)

然后,为了使按钮真正执行某些操作,它们需要改变您的幅度。首先将幅度存储为实例的属性。还存储在属性中以便稍后使其移动。

您的 a 和 ptr1 只是方法内的变量,方法完成后将被清除。通过使用,self.它们成为可以从类中的其他方法使用的实例属性。

def __init__(self):
    ...
    self.amplitude = 10
    self.t = 0
    ...
Run Code Online (Sandbox Code Playgroud)

然后您可以在连接到按钮的方法中更改幅度。

def on_increasebutton_clicked(self):
    print ("Amplitude increased")
    self.amplitude += 1
    self.updateplot()
Run Code Online (Sandbox Code Playgroud)

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

    self.timer = pg.QtCore.QTimer()
    self.timer.timeout.connect(self.moveplot)
    self.timer.start(50)
Run Code Online (Sandbox Code Playgroud)

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

def moveplot(self):
    self.t+=1
    self.updateplot()
Run Code Online (Sandbox Code Playgroud)

然后将其放在一起创建和 updateplot 方法,该方法使用之前创建的属性。

def updateplot(self):
    print "Update"
    data1 = self.amplitude*np.sin(np.linspace(0,30,121)+self.t)
    self.plotcurve.setData(data1)
Run Code Online (Sandbox Code Playgroud)

最后你会得到这样的东西

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()
Run Code Online (Sandbox Code Playgroud)