问题描述
我使用 pyqtgraph.examples
中的滚动图"示例作为模板来创建一个绘图,它显示了一个连续的正弦波.现在,我想使用按钮来改变正弦波的幅度.这就是我使用另一种结构的原因(见下面的代码).这不起作用,它只绘制静态正弦波.如何使用我的更新函数绘制连续的正弦波?如何使用按钮改变振幅?我已经检查了这个和这个没有成功.
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()
推荐答案
所以这不是一个真正的 pyqtgraph 问题.大多数情况下,您需要阅读 Python 类和 pyqt 基础知识.但我会尽力为您提供一些快速解决方案,希望您能在此过程中学到一些东西.
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.
为了使您的按钮工作,您需要将它们连接到方法.尝试在此处查看答案:https://stackoverflow.com/a/8763339/4328381
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
你需要这样的东西
def qt_connections(self):
self.increasebutton.clicked.connect(self.on_increasebutton_clicked)
self.decreasebutton.clicked.connect(self.on_decreasebutton_clicked)
然后让按钮真正做一些事情,他们需要改变你的振幅.首先将振幅存储为实例的属性.还要存储一个 t 属性,以便以后移动.
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.
你的 a 和 ptr1 只是方法内部的变量,一旦方法完成就会被清除.通过使用 self.
,它们成为可以从类中的其他方法使用的实例属性.
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()
然后要使其连续,您首先需要确保计时器正常工作.尝试在其中添加一个 print("test")
,您会看到它没有.您需要保留它的引用,否则它会被清理.
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)
要使正弦连续移动,您需要在连接到计时器的方法中移动它,您可以简单地创建一个 moveplot 方法.
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()
然后把它放在一起使用之前创建的属性的 create 和 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()
这篇关于如何更新实时绘图并使用按钮在 pyqtgraph 中进行交互?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!