|
| 1 | +#!/usr/bin/env python |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | +""" |
| 4 | +This example demonstrates a random walk with pyqtgraph. |
| 5 | +""" |
| 6 | + |
| 7 | +import sys |
| 8 | +import numpy as np |
| 9 | +import pyqtgraph as pg |
| 10 | +from pyqtgraph.Qt import QtGui, QtCore |
| 11 | +from numpy_buffer import RingBuffer # https://github.com/scls19fr/numpy-buffer |
| 12 | + |
| 13 | +class RandomWalkPlot: |
| 14 | + def __init__(self, win): |
| 15 | + #self.plot = pg.plot() |
| 16 | + self.plot = win.addPlot(title="Updating plot") |
| 17 | + |
| 18 | + self.ptr = 0 |
| 19 | + |
| 20 | + #pen = 'r' |
| 21 | + pen = pg.mkPen('b', style=QtCore.Qt.SolidLine) |
| 22 | + self.curve = self.plot.plot(pen=pen, symbol='+') |
| 23 | + self.timer = QtCore.QTimer() |
| 24 | + self.timer.timeout.connect(self.update) |
| 25 | + self.timer.start(50) |
| 26 | + |
| 27 | + self.value = 1000 # initial value |
| 28 | + N = 100 # number of elements into circular buffer |
| 29 | + |
| 30 | + self.data_y = RingBuffer(N, self.value) |
| 31 | + |
| 32 | + |
| 33 | + def update(self): |
| 34 | + self.value += np.random.uniform(-1, 1) |
| 35 | + |
| 36 | + self.data_y.append(self.value) |
| 37 | + |
| 38 | + self.curve.setData(y=self.data_y) # size is increasing up to N |
| 39 | + #self.curve.setData(y=self.data_y.all[::-1]) # size is always N |
| 40 | + |
| 41 | + #if self.ptr == 0: |
| 42 | + # self.plot.enableAutoRange('xy', False) ## stop auto-scaling after the first data set is plotted |
| 43 | + #self.ptr += 1 |
| 44 | + |
| 45 | +def main(): |
| 46 | + #QtGui.QApplication.setGraphicsSystem('raster') |
| 47 | + app = QtGui.QApplication(sys.argv) |
| 48 | + |
| 49 | + #mw = QtGui.QMainWindow() |
| 50 | + #mw.resize(800,800) |
| 51 | + |
| 52 | + pg.setConfigOption('background', 'w') |
| 53 | + pg.setConfigOption('foreground', 'k') |
| 54 | + |
| 55 | + win = pg.GraphicsWindow(title="Basic plotting examples") |
| 56 | + win.resize(1000, 600) |
| 57 | + win.setWindowTitle('plot') |
| 58 | + |
| 59 | + # Enable antialiasing for prettier plots |
| 60 | + pg.setConfigOptions(antialias=True) |
| 61 | + |
| 62 | + upl = RandomWalkPlot(win) |
| 63 | + |
| 64 | + ## Start Qt event loop unless running in interactive mode or using pyside. |
| 65 | + if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'): |
| 66 | + QtGui.QApplication.instance().exec_() |
| 67 | + |
| 68 | +if __name__ == '__main__': |
| 69 | + main() |
0 commit comments