I am making a scrolling graph which will plot real-time sensor data, with time on the x axis. I am a bit confused by the behavior of tickStrings.
My code is based on the example below (from here). As the number of points plotted increases, the number of x axis strings varies - sometimes it increases and sometimes it decreases . It stabilizes once the deque is full length and the 'scrolling' begins.
Is it possible to keep the spacing between tick strings the same as the number of plotted points increases? I guess that it might be possible use an approach where blank tick strings are replaced as new data is added, but don't know how to do that.
Edit: An example of what I wish to achieve is here.
import sys
import numpy as np
import datetime
from PyQt5.QtCore import QTime, QTimer
from pyqtgraph.Qt import QtGui
import pyqtgraph as pg
from collections import deque
import time
class TimeAxisItem(pg.AxisItem):
def __init__(self, *args, **kwargs):
super(TimeAxisItem, self).__init__(*args, **kwargs)
def tickStrings(self, values, scale, spacing):
return [int2dt(value).strftime("%H:%M:%S") for value in values]
def int2dt(ts):
return(datetime.datetime.fromtimestamp(ts))
class MyApplication(QtGui.QApplication):
def __init__(self, *args, **kwargs):
super(MyApplication, self).__init__(*args, **kwargs)
self.t = QTime()
self.t.start()
maxlen = 100
self.data_x = deque(maxlen=maxlen)
self.data_y = deque(maxlen=maxlen)
self.win = pg.GraphicsLayoutWidget()
self.win.resize(1000,600)
self.plot = self.win.addPlot(title='Scrolling real-time plot', axisItems={'bottom': TimeAxisItem(
orientation='bottom')})
self.curve = self.plot.plot()
self.tmr = QTimer()
self.tmr.timeout.connect(self.update)
self.tmr.start(1000)
self.y = 100
self.win.show()
def update(self):
x = int(time.time())
self.y = self.y + np.random.uniform(-1, 1)
self.data_x.append(x)
self.data_y.append(self.y)
time.sleep(2)
print(self.data_x)
print(self.data_y)
self.curve.setData(x=list(self.data_x), y=list(self.data_y))
def main():
app = MyApplication(sys.argv)
sys.exit(app.exec_())
if __name__ == '__main__':
main()