Pyqtgraph实时p

2024-04-27 14:25:49 发布

您现在位置:Python中文网/ 问答频道 /正文

我想用x轴移动绘图,但现在所有的绘图点,都在一个屏幕上,起点不移动

窗口代码-主窗口代码。函数updateplot每5s运行一次并设置新数据。在

import sys
from PyQt5 import QtWidgets, QtCore, QtGui
import stack
import pyqtgraph as pgt
from random import uniform, normalvariate
import random

class ExampleApp(QtWidgets.QMainWindow, stack.Ui_MainWindow):
    def __init__(self):
        super().__init__()
        self.setupUi(self)
        self.graphicsView.getAxis('left').setLabel('Data Value', color='#0000ff')
        self.graphicsView.getAxis('bottom').setLabel('time', 's')
        self.graphicsView.showGrid(x=True, y=True)
        self.graphicsView.setYRange(0,10)
        self.graphicsView.addLine(y=5,pen=pgt.mkPen('y'))
        self.graphicsView.addLine(y=7,pen=pgt.mkPen('r'))
        self.curve = self.graphicsView.plot()
        self.L = []
        self.timer = QtCore.QTimer()
        self.timer.timeout.connect(self.updateplot)
        self.timer.start(500)

    def getdata(self):
        frequency = 0.5
        noise = random.normalvariate(0., 1.)
        new = 10.*math.sin(time.time()*frequency*2*math.pi) + noise
        return new

    def updateplot(self):
        val = round(uniform(0,10), 2)
        self.L.append(val)
        self.curve.setData(self.L)
        #QtGui.QGuiApplication.processEvents()


def main():
    app = QtWidgets.QApplication(sys.argv)
    window = ExampleApp()
    window.show()
    app.exec_()

if __name__ == '__main__':
    main()

设计者生成的设计代码(堆栈.py): 使用PlotWidget代替的设计器

^{pr2}$

Tags: 代码fromimportself绘图timemaindef
1条回答
网友
1楼 · 发布于 2024-04-27 14:25:49

pyqtgraph将显示列表的数据,在您的示例中,列表始终具有第一个元素,因此您始终可以看到该数据。解决方案是使用具有最大长度的deque,因此将使用最后的元素。在

import sys
from collections import deque
from PyQt5 import QtWidgets, QtCore, QtGui
import stack
import pyqtgraph as pg
import random


class ExampleApp(QtWidgets.QMainWindow, stack.Ui_MainWindow):
    def __init__(self):
        super().__init__()
        self.setupUi(self)
        self.graphicsView.getAxis('left').setLabel('Data Value', color='#0000ff')
        self.graphicsView.getAxis('bottom').setLabel('time', 's')
        self.graphicsView.showGrid(x=True, y=True)
        self.graphicsView.setYRange(0,10)
        self.graphicsView.addLine(y=5,pen=pg.mkPen('y'))
        self.graphicsView.addLine(y=7,pen=pg.mkPen('r'))
        self.curve = self.graphicsView.plot()
        self.L = deque([0], maxlen=10)
        self.t = deque([0], maxlen=10)
        self.timer = QtCore.QTimer(self)
        self.timer.timeout.connect(self.updateplot)
        self.timer.start(500)

    def updateplot(self):
        val = round(random.uniform(0,10), 2)
        self.L.append(val)
        self.t.append(self.t[-1]+1)
        self.curve.setData(self.t, self.L)

相关问题 更多 >