pyQtGraph检测小部件边界并开始滚动

时间:2017-10-26 09:01:53

标签: arrays python-3.x numpy pyqt5 pyqtgraph

我试图用pyQtGraph绘制一个实时图表(一个关于股票交易演变的图表),并且有一些我无法解决检查示例的问题:

  1. 我希望图形从左到右开始绘制(这是默认情况下发生的事情),当它到达边界框的右侧而不是调整它以使所有新数据适合我希望它滚动使新数据从右侧输入,旧数据从左侧消失。

  2. 我知道将数据附加到numpy数组会创建一个新的数组实例。我不想要这个。有没有办法告诉pyQtGraph图只是获取numpy数组范围内的数据?为了exmaple,我可以最初实例化一个10000浮点数组并告诉pyQtGraph只绘制前100个浮点数吗?

  3. 另一方面,我遇到过我可以就地修改数组并移动数字来模拟滚动。有没有办法让pyQtGraph使用numpy数组作为环?这样我只需要告诉图表从一个索引开始,一切都可以在没有分配等情况下工作......

  4. 这是我到目前为止的代码,非常简单:

    class Grapher(QtWidgets.QMainWindow, Ui_MainWindow):
        def __init__(self):
            QtWidgets.QMainWindow.__init__(self)
            Ui_MainWindow.__init__(self)
            self.setupUi(self)
    
            self.graphicsView.setTitle('My Graph')
            self.graphicsView.setLabel('bottom', 'X axis')
            self.graphicsView.setLabel('left', 'Y axis')
            self.graphicsView.setLimits(xMin=0, yMin=0)
            self.graphicsView.showGrid(x=True, y=True)
            self.x=np.array();
            self.y=np.array();
    
            self._timer=QtCore.QTimer()
            self._timer.timeout.connect(self.update)
            self._timer.start(1000)
            self._timerCounter=0;
    
        def update(self):
            self.x=np.append(self.x, [self._timerCounter]);
            self.y=np.append(self.y, [math.sin(self._timerCounter)])
            self.graphicsView.plot(self.x, self.y)
            self._timerCounter+=1;
    

    提前致谢。

1 个答案:

答案 0 :(得分:0)

我就是这样做的。比方说,如果你有一个1000点的阵列,但你只想绘制200点:

class Grapher(QtWidgets.QMainWindow, Ui_MainWindow):

    def __init__(self):
        QtWidgets.QMainWindow.__init__(self)
        Ui_MainWindow.__init__(self)
        self.setupUi(self)

        self.graphicsView.setTitle('My Graph')
        self.graphicsView.setLabel('bottom', 'X axis')
        self.graphicsView.setLabel('left', 'Y axis')
        self.graphicsView.setLimits(xMin=0, yMin=0)
        self.graphicsView.showGrid(x=True, y=True)
        self.plot = self.graphicsView.plot()

        self.ptr = 0
        self.x = np.zeros(1000)
        self.y = np.zeros(1000)
        self.xPlot = np.zeros(200)
        self.yPlot = np.zeros(200)

        self._timer = QtCore.QTimer()
        self._timer.timeout.connect(self.update)
        self._timer.start(1000)
        self._timerCounter = 0

    def update(self):

        xNew = self._timerCounter
        yNew = math.sin(xNew)
        self.y[self.ptr] = math.sin(self._timerCounter)
        self.x[self.ptr] = self._timerCounter

        if self.ptr < 200:
            # Normal assignment
            self.yPlot[self.ptr] = self.y[self.ptr]
            self.xPlot[self.ptr] = self.x[self.ptr]
            self.plot.setData(self.xPlot[1:self.ptr + 1], 
                              self.yPlot[1:self.ptr + 1])

        else:
            # Shift arrays and then assign
            self.yPlot[:-1] = self.yPlot[1:]
            self.yPlot[-1] = self.y[self.ptr]
            self.xPlot[:-1] = self.xPlot[1:]
            self.xPlot[-1] = self.x[self.ptr]
            self.plot.setData(self.xPlot, self.yPlot)

        self.ptr += 1
相关问题