pyQtGraph 检测小部件边界并开始滚动
pyQtGraph detect widget bounds and start scrolling
我正在尝试使用 pyQtGraph 绘制实时图表(关于股票交易演变的图表)并且有一些我无法通过检查示例解决的问题:
我希望图形从左到右开始绘制(这是默认情况下发生的情况)并且当它到达边界框的右侧而不是调整它的大小以使所有新数据适合我会喜欢它滚动使新数据从右边进入,旧数据从左边消失。
我知道将数据附加到 numpy 数组会创建一个新的数组实例。我不想要这个。有没有办法告诉 pyQtGraph plot 只获取 numpy 数组范围内的数据?例如,我可以最初实例化一个包含 10000 个浮点数的数组并告诉 pyQtGraph 只绘制前 100 个浮点数吗?
另一方面,我发现我可以就地修改数组并移动数字来模拟滚动。有没有办法让 pyQtGraph 使用 numpy 数组作为环?这样我只需要告诉图表从一个索引开始,一切都可以在没有分配的情况下工作,等等...
这里是我目前的代码,非常简单:
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;
提前致谢。
我就是这样做的。例如,如果您有一个包含 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
我正在尝试使用 pyQtGraph 绘制实时图表(关于股票交易演变的图表)并且有一些我无法通过检查示例解决的问题:
我希望图形从左到右开始绘制(这是默认情况下发生的情况)并且当它到达边界框的右侧而不是调整它的大小以使所有新数据适合我会喜欢它滚动使新数据从右边进入,旧数据从左边消失。
我知道将数据附加到 numpy 数组会创建一个新的数组实例。我不想要这个。有没有办法告诉 pyQtGraph plot 只获取 numpy 数组范围内的数据?例如,我可以最初实例化一个包含 10000 个浮点数的数组并告诉 pyQtGraph 只绘制前 100 个浮点数吗?
另一方面,我发现我可以就地修改数组并移动数字来模拟滚动。有没有办法让 pyQtGraph 使用 numpy 数组作为环?这样我只需要告诉图表从一个索引开始,一切都可以在没有分配的情况下工作,等等...
这里是我目前的代码,非常简单:
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;
提前致谢。
我就是这样做的。例如,如果您有一个包含 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