Pyqtgraph现场剧情

时间:2018-08-28 10:11:11

标签: python python-3.x pyqt5 pyqtgraph

我想通过x轴移动绘图,但是现在所有绘图点都停留在一个屏幕上,并且起点不动

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

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

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

的设计器
from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(929, 429)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.graphicsView = PlotWidget(self.centralwidget)
        self.graphicsView.setGeometry(QtCore.QRect(9, 9, 901, 321))
        self.graphicsView.setObjectName("graphicsView")
        MainWindow.setCentralWidget(self.centralwidget)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "Data"))

from pyqtgraph import PlotWidget

1 个答案:

答案 0 :(得分:0)

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

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)