[英]PyQtGraph: Issue looping through plotting slices of data
我有一个 python class 生成大小为 10000 的numpy
数组并对其执行一些计算。 class 有两个 plot 方法并且都使用pyqtgraph
我想知道如何遍历数据段(一次 200 个样本)并向用户显示处理后的数据,并等到用户按下任意键后再绘制下一个 200 个样本?
我希望能够在不关闭 Qt window 的情况下更新 plot 以实现更高效的性能,只需更新已经绘制的 Z495BEB668CFFDE891311 的内容即可。
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()
我将不胜感激任何帮助或提示。
要检测键盘事件,有几个选项取决于具体目标:
如果您想在焦点位于小部件中时检测任何键的按下,则只需覆盖 window 的 keyPressEvent 方法。
即使 window 没有键,如果您想检测任何键的按下,那么您必须使用操作系统库,幸运的是,在 python 中有包装器,例如 pyinput、键盘等。
另一方面,逻辑是获取数据片段,所以要做到这一点,只需使用生成器 function。
考虑第一种情况,解决方案是:
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_()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.