PyQtGraph。循环绘制数据片的问题。

问题描述 投票:0回答:1

我有一个python类,它可以生成一个 numpy 大小为10000的数组,并对其进行一些计算。该类有两个绘图方法,都使用 pyqtgraph

  • 绘制整个数据
  • 一次性绘制数据段(200个样本)。

我想知道如何才能循环处理数据段(一次200个样本),并将处理后的数据显示给用户,等到用户按下任何键后再绘制下一个200个样本?

我希望能够在不关闭Qt窗口的情况下更新绘图,以实现更高效的性能,只需更新已经绘制的对象的内容。

import numpy as np
from pyqtgraph.Qt import QtCore, QtGui
import pyqtgraph as pg

class testqt:

    def __init__(self):
        self.data = np.random.randn(10000) # for the sake of providing a MWE
        self.do_some_processing()

    def do_some_processing(self):
        self.data_processed = 2*self.data

    def plot_entire_processed_data(self):
        # Plot the entire processed data
        win = pg.GraphicsLayoutWidget(show=True)
        p = win.addPlot()
        curve_data = p.plot(self.data_processed)
        QtGui.QApplication.instance().exec_()

    def plot_processed_data_every_200(self):
        # animate the processed data in segments of 200 samples
        win = pg.GraphicsLayoutWidget(show=True)
        p = win.addPlot()
        curve_data = p.plot(self.data_processed[:200])

        for i in range(200, len(self.data_processed), 200):
            curve_data.setData(self.data_processed[i:i+200])
            # How can I pause here and use keyboard to move to the next 200 samples?
            # I would like to be able to visually evaluate each segment first and then 
            # press any key to see the next segment


        QtGui.QApplication.instance().exec_() # unfortunately, this only plot the last 200 samples


a = testqt()
a.plot_entire_processed_data()
a.plot_processed_data_every_200()

我希望得到任何帮助或提示。

python pyqt5 pyqtgraph qtgui qapplication
1个回答
2
投票

要检测键盘事件,有几个选项,取决于具体的目标。

  • 如果你想检测当焦点在小组件中时是否有任何按键被按下,那么只需覆盖窗口的keyPressEvent方法。

  • 如果你想在窗口没有键的时候也能检测到任何键的按压,那么就必须使用操作系统的库,在python中幸好有pyinput、键盘等封装器。

另一方面,逻辑是获取数据的碎片,所以要做到这一点,只需使用生成函数即可。

考虑到第一种情况,解决办法是。

import numpy as np
from pyqtgraph.Qt import QtCore, QtGui
import pyqtgraph as pg


def iter_by_step(data, step):
    for i in range(0, len(data), step):
        yield data[i : i + step]


class GraphicsLayoutWidget(pg.GraphicsLayoutWidget):
    def __init__(self, parent=None):
        super().__init__(parent)

        self.data = np.random.randn(10000)
        self.iter = None

        curve = self.addPlot()
        self.p = curve.plot()

    def do_some_processing(self):
        self.data_processed = 2 * self.data
        self.iter = iter_by_step(self.data_processed, 200)
        self.plot_processed_data_every_200()

    def keyPressEvent(self, event):
        super().keyPressEvent(event)
        if self.iter is not None:
            self.plot_processed_data_every_200()

    def plot_processed_data_every_200(self):
        try:
            data = next(self.iter)
        except StopIteration:
            pass
        else:
            self.p.setData(data)


if __name__ == "__main__":

    w = GraphicsLayoutWidget()
    w.show()
    w.do_some_processing()

    QtGui.QApplication.instance().exec_()
© www.soinside.com 2019 - 2024. All rights reserved.