我在使用pygtgraph滚动绘图时遇到问题
预期结果与pyqtgraph-examples-scrolling plots-plot5非常相似
X值是时间,可以通过简单的函数生成。Y值是随机值
每10秒采样一个区块,每个绘图最多可以有30秒的样本,这意味着3个区块当前绘图窗口仅显示最近10秒的样本
例如,现在总共有60秒的样本:
我当前的代码如下,它只能显示最新的30秒数据
import pyqtgraph as pg
from pyqtgraph.Qt import QtCore, QtGui
import numpy as np
import random
win = pg.GraphicsLayoutWidget(show=True)
win.setWindowTitle('Scrolling Plots')
p1 = win.addPlot()
p1.setYRange(0,10)
xVal = [0]
yVal = [0]
def genTime(): # used to generate time
t = 0
while True:
t += np.random.random_sample()
yield t
t = np.ceil(t)
xTime = genTime()
#=====================================================
viewSize = 10 # current window show only latest 10s data
plotSize = 30 # plot 30s data -> 3 chunk
lstCurves = [] # List for Curves
def update():
global p1, xVal, yVal, lstCurves
#for c in lstCurves:
# c.setPos(xVal[-1], 0)
i = np.ceil(xVal[-1]) % viewSize # e.g. when time is 9.2s -> one 10s view size is full, append to curves list as one chunk
if i == 0:
curve = p1.plot()
lstCurves.append(curve)
xValLast = xVal[-1]
yValLast = yVal[-1]
xVal = [xValLast]
yVal = [yValLast]
while len(lstCurves) > 3: # max 3 chunk (30 s)
p1.removeItem(lstCurves.pop(0)) # remove the oldest 10s
else:
curve = lstCurves[-1] # latest 10s curve
xVal.append(next(xTime))
yVal.append(random.randint(0,9))
curve.setData(xVal, yVal)
print(len(lstCurves))
#======================================================
timer = pg.QtCore.QTimer()
timer.timeout.connect(update)
timer.start(1000)
## Start Qt event loop unless running in interactive mode or using pyside.
if __name__ == '__main__':
import sys
if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'):
QtGui.QApplication.instance().exec_()
我尝试过使用curve.setPos(xx, 0)
,看起来整个曲线都在沿x轴移动,但是x值和Y值之间的映射关系被破坏了
我还尝试使用setXRange()
动态更改update()
func中的x轴显示范围。但在这种情况下,我不能再使用鼠标将x轴拖回查看旧数据
我的英语不好,希望你能理解我的问题。如有任何建议,将不胜感激
问题
您的代码不执行所需操作的原因是:
update
函数中使用setRange()
方法时,每次向数据添加另一个值时,它都会强制该范围。然后拖动将无法按您所需的方式工作你能做什么
嗯,从我的角度来看,使用鼠标拖动来可视化其他数据不是很方便,我建议使用外部小部件来控制要查看的数据范围,例如,滑块、滚动条、旋转框等等。。。 一个
QScrollBar
可以完成这项工作,而且它在GUI中看起来很美观在我的替代解决方案之前,我有一个建议:
global
,并且可以将小部件用于其他目的李>替代解决方案
试试这个:
可能是这样的:
相关问题 更多 >
编程相关推荐