Hello everyone!

Im trying to put together a system, where I can plot data read from files in 
real time.

I wrote some code starting from the scrolling plot examples.

The correct plot is attached as the correct_plot.png file.

I have downsampling on:
                curve.setDownsampling(ds=100, method="subsample")
All other downsampleing methods caused glitching when I resized the plot window.

Inside the update() function I check if the files is ready (another process 
writes to these files):

    # if the next file doesnt exist, the current is still being written to
    if not os.path.exists(next_file_name):
        print("File is not ready yet")
        return

But whenever I reach this state, the plot starts glitching again. (See 
glitch.png)

I don't know what causes this. The update() function should run similarly, as 
this check is at the beginning, and if the file isn't ready, I simply return 
from it.
See my code attached as well.

Sidenote:
                I've seen this Github issue: ScrollingPlots Plot 3 Not 
Functioning · Issue #1916 · pyqtgraph/pyqtgraph · 
GitHub<https://github.com/pyqtgraph/pyqtgraph/issues/1916>
                Apparently making scrolling plots shouldn't be done using 
SetPos(). What should I use instead?

What am I doing wrong? If I change - or remove - the downsampling, it just gets 
worse, and the glitching starts even in "normal" conditions, e.g. without 
returning prematurely because the file isn't ready.

Thanks in advance,

Gergely

-- 
You received this message because you are subscribed to the Google Groups 
"pyqtgraph" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/pyqtgraph/AM6PR07MB6070030B3B7C5868706ADAEDFCF79%40AM6PR07MB6070.eurprd07.prod.outlook.com.
import pyqtgraph as pg
from pyqtgraph.Qt import QtCore, QtGui
import numpy as np
import os

chunk_size = 1000.0 # e.g. filesize
timeout = 130 # msec
max_chunks = 10 * 1000 / timeout # every chunk is visible for 10 seconds


win = pg.GraphicsLayoutWidget(show=True)
p5 = win.addPlot(colspan=10)
p5.setLabel('bottom', 'Samples', 'S')
p5.setLabel('left', 'TIE', 'nsec')
p5.setXRange(- int(max_chunks)*int(chunk_size), 0)
p5.setYRange(-0.1, 0.1, 0)
curves = []
data5 = np.empty((int(chunk_size),2))

def update():
    global p5, data5, curves

    # if the next file doesnt exist, the current is still being written to
    if not os.path.exists(next_file_name):
        print("File is not ready yet")
        return

    curve = p5.plot()
    curve.setDownsampling(ds=100, method="subsample")
    curves.append(curve)
    last = data5[-1]
    data5[0] = last
    while len(curves) > max_chunks:
        c = curves.pop(0)
        p5.removeItem(c)


    """
        Moving the plot:
        every time update() is called, each chunk of data is shifted by the 
chunk_size
    """
    curve_pos_offset = len(curves)
    for c in curves:
        c.setPos( - curve_pos_offset * chunk_size, 0 )
        curve_pos_offset -= 1


    data5 = np.empty((int(chunk_size),2))
    data5[:,0] = list(range(0, int(chunk_size)))

    try:
        with open(FILENAME, "r") as f:
            print("Reading " + FILENAME)
            i = 0
            for line in f:
                try:
                    data5[i, 1] = line.split(" ")[1]
                except Exception as e:
                    print(e)
                    print("Line: " + line)
                i += 1
        curve.setData(x=data5[:, 0], y=data5[:, 1])
    except Exception as e:
        print(e)
        print("Exiting...")
        exit()

timer = pg.QtCore.QTimer()
timer.timeout.connect(update)
timer.start(int(timeout)) # msec

if __name__ == '__main__':
    pg.exec()

Reply via email to