Live Data Monitor: PyQtGraph

纵饮孤独 提交于 2019-12-21 02:07:06

问题


I am working on a project where I will have to analyse signals coming from a device. I have a library working which gets me data from the device. As of now, I am collecting the data and then plotting it. I am interested in building a live data monitor which can plot a graph in real time. Upon searching, I figured out PyQtGraph is ideal for the task. I am not familiar with Qt, so I am looking for examples which I can modify to my needs. Some examples given in PyQtGraph docs update the plot real-time BUT I need something like a live monitor- where the graph is moving towards the right as it keeps receiving data.

If it it something like a known continuous function, I can update the input x - w*t with t being the time so as to get the wave moving towards right. But this is discrete data, so I am not sure about how to get it working using PyQtGraph. So it would be great if someone could give some pointers on how to go about.

As of now this is what I have

Code

app = QtGui.QApplication([])
#mw = QtGui.QMainWindow()
#mw.resize(800,800)

win = pg.GraphicsWindow(title="Basic plotting examples")
win.resize(1000,600)
win.setWindowTitle('pyqtgraph example: Plotting')

# Enable antialiasing for prettier plots
pg.setConfigOptions(antialias=True)

p6 = win.addPlot(title="Updating plot")
curve = p6.plot(pen='r')
X_axis = numpy.linspace(0,100,12800)
#'data' is my required y_axis containing 12800 values
ydata = np.array_split(data,50)
xdata = np.array_split(X_axis,50)
ptr = 0
def update():
    global curve, data, ptr, p6
    curve.setData(xdata[ptr%50],ydata[ptr%50])
    ptr += 1
timer = QtCore.QTimer()
timer.timeout.connect(update)
timer.start(1000)

This is updating the data for every 2-second interval, but I want it to move towards the right.


回答1:


To make a plot scroll, you have three options:

  1. Scroll the raw data and re-plot (see numpy.roll)

    curve = plotItem.plot(data)
    data = np.roll(data, 1)  # scroll data
    curve.setData(data)      # re-plot
    
  2. Move the plot curve so that it slides across the view:

    curve = plotItem.plot(data)
    curve.setPos(x, 0)  # Move the curve
    
  3. Move the view region such that the plot curve appears to scroll

    curve = plotItem.plot(data)
    plotItem.setXRange(x1, x2)  # Move the view
    

The scrolling-plots example (currently only in the development version) demonstrates each of these:



来源:https://stackoverflow.com/questions/24197910/live-data-monitor-pyqtgraph

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!