Synthesizing efficient PyQtGraph strip chart approach
The thought clarifies that maintaining a double-sized ring buffer with mirrored values enables contiguous y-data views without copies, letting setData update the GPU vertex array without reallocations; combining this with precomputed static x values and OpenGL acceleration in PyQtGraph supports real-time 100Hz scrolling plots while minimizing overhead.Detailing PyQtGraph fast update pattern with ring buffer
The thought confirms that PyQtGraph’s recommended fast-update method involves reusing a preallocated buffer updated in place and calling setData each frame with the same-sized arrays, enabling internal GPU vertex buffers to update without reallocating. Using a double-sized ring buffer to provide a contiguous view avoids costly array rolling, and enabling useOpenGL=True accelerates drawing while maintaining a stable vertex array size.
One of the nice things about pyqtgraph is that you do not have to build any custom shaders – if you give it a fixed‑size, contiguous ndarray and turn on the OpenGL backend, it will create a VBO for you and only call glBufferSubData when the contents change.
The two tricks for a strip chart are:
- keep the array length constant, so the VBO never has to be resized, and
- avoid
np.roll/np.concatenate in your update loop. A common pattern is to allocate twice as much storage and mirror the samples so the “last N” window is always a contiguous slice.
The following example creates a 10 kpoint buffer, updates it at 100 Hz and scrolls the view. All of the X coordinates are precomputed once; only the Y values are mutated in place:
import sys
import numpy as np
from PyQt5 import QtCore, QtWidgets
import pyqtgraph as pg
SAMPLE_RATE = 100.0 # Hz
N_POINTS = 10_000
DT = 1.0 / SAMPLE_RATE
pg.setConfigOptions(useOpenGL=True, antialias=True) # use the GL backend
class StripChart(QtWidgets.QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("10k point strip chart")
plot = pg.PlotWidget()
plot.showGrid(x=True, y=True)
plot.setXRange(-N_POINTS*DT, 0.0)
plot.setLimits(xMin=-N_POINTS*DT, xMax=0)
self.setCentralWidget(plot)
# Precompute x coordinates once
self.x = np.linspace(-N_POINTS*DT, 0.0, N_POINTS, dtype=np.float32)
# Double-length buffer to make a contiguous view of the last N samples
self.ybuf = np.zeros(N_POINTS*2, dtype=np.float32)
self.head = 0
# Create the curve; skipFiniteCheck/clipToView cut down overhead
self.curve = plot.plot(pen='y', skipFiniteCheck=True, clipToView=True)
# Simulate an incoming data source at 100 Hz
self.timer = QtCore.QTimer(self)
self.timer.timeout.connect(self.on_new_sample)
self.timer.start(int(DT*1000))
def on_new_sample(self):
# In a real application, pull your measurement here
t = self.head * DT
new_y = np.sin(2*np.pi*1.0*t) # dummy data
i = self.head % N_POINTS
# write into both halves of the double buffer
self.ybuf[i] = new_y
self.ybuf[i+N_POINTS] = new_y
# Take a view of the last N_POINTS samples; no copy is made
start = i+1 # end of window is current head
yview = self.ybuf[start:start+N_POINTS]
# Update curve. Length is constant, so the underlying VBO is reused.
self.curve.setData(self.x, yview)
self.head += 1
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
w = StripChart()
w.resize(800, 400)
w.show()
sys.exit(app.exec_())
Because self.x is fixed and yview is just a different view on the same ybuf storage, setData sees the same length and dtype every time; with useOpenGL=True the vertex array in GPU memory is updated in place instead of being reallocated. At 100 Hz and 10 000 points this runs comfortably on modest hardware.