Created
May 7, 2014 11:19
-
-
Save friendzis/4e98ebe2cf29c0c2c232 to your computer and use it in GitHub Desktop.
Simple implementation of TimeAxisItem and test program
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from pyqtgraph.Qt import QtGui, QtCore | |
import numpy as np | |
import pyqtgraph as pg | |
from PySide.QtCore import QTime, QTimer | |
from collections import deque | |
t = QTime() | |
t.start() | |
data = deque(maxlen=20) | |
class TimeAxisItem(pg.AxisItem): | |
def __init__(self, *args, **kwargs): | |
super().__init__(*args, **kwargs) | |
def tickStrings(self, values, scale, spacing): | |
# PySide's QTime() initialiser fails miserably and dismisses args/kwargs | |
return [QTime().addMSecs(value).toString('mm:ss') for value in values] | |
app = QtGui.QApplication([]) | |
win = pg.GraphicsWindow(title="Basic plotting examples") | |
win.resize(1000,600) | |
plot = win.addPlot(title='Timed data', axisItems={'bottom': TimeAxisItem(orientation='bottom')}) | |
curve = plot.plot() | |
def update(): | |
global plot, curve, data | |
data.append({'x': t.elapsed(), 'y': np.random.randint(0, 100)}) | |
x = [item['x'] for item in data] | |
y = [item['y'] for item in data] | |
curve.setData(x=x, y=y) | |
tmr = QTimer() | |
tmr.timeout.connect(update) | |
tmr.start(800) | |
if __name__ == '__main__': | |
import sys | |
if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'): | |
QtGui.QApplication.instance().exec_() |
A random walk example with datetime as x axis:
import sys
import numpy as np
import datetime
from PyQt4.QtCore import QTime, QTimer
from pyqtgraph.Qt import QtGui, QtCore
import pyqtgraph as pg
from collections import deque
import pytz
UNIX_EPOCH_naive = datetime.datetime(1970, 1, 1, 0, 0) #offset-naive datetime
UNIX_EPOCH_offset_aware = datetime.datetime(1970, 1, 1, 0, 0, tzinfo = pytz.utc) #offset-aware datetime
UNIX_EPOCH = UNIX_EPOCH_naive
TS_MULT_us = 1e6
def now_timestamp(ts_mult=TS_MULT_us, epoch=UNIX_EPOCH):
return(int((datetime.datetime.utcnow() - epoch).total_seconds()*ts_mult))
def int2dt(ts, ts_mult=TS_MULT_us):
return(datetime.datetime.utcfromtimestamp(float(ts)/ts_mult))
def dt2int(dt, ts_mult=TS_MULT_us, epoch=UNIX_EPOCH):
delta = dt - epoch
return(int(delta.total_seconds()*ts_mult))
def td2int(td, ts_mult=TS_MULT_us):
return(int(td.total_seconds()*ts_mult))
def int2td(ts, ts_mult=TS_MULT_us):
return(datetime.timedelta(seconds=float(ts)/ts_mult))
class TimeAxisItem(pg.AxisItem):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
def tickStrings(self, values, scale, spacing):
# PySide's QTime() initialiser fails miserably and dismisses args/kwargs
#return [QTime().addMSecs(value).toString('mm:ss') for value in values]
return [int2dt(value).strftime("%H:%M:%S.%f") for value in values]
class MyApplication(QtGui.QApplication):
def __init__(self, *args, **kwargs):
super(MyApplication, self).__init__(*args, **kwargs)
self.t = QTime()
self.t.start()
maxlen = 200
self.data_x = deque(maxlen=maxlen)
self.data_y = deque(maxlen=maxlen)
self.win = pg.GraphicsWindow(title="Basic plotting examples")
self.win.resize(1000,600)
self.plot = self.win.addPlot(title='Timed data', axisItems={'bottom': TimeAxisItem(orientation='bottom')})
#self.plot.setYRange(0, 150)
self.curve = self.plot.plot()
self.tmr = QTimer()
self.tmr.timeout.connect(self.update)
self.tmr.start(100)
self.y = 100
def update(self):
#self.data.append({'x': self.t.elapsed(), 'y': np.random.randint(0, 100)})
x = now_timestamp()
self.y = self.y + np.random.uniform(-1, 1)
self.data_x.append(x)
self.data_y.append(self.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()
wonderful as @scls19fr wrote.Thx
fix to work with Python 2.7.12
import sys
import numpy as np
import datetime
from PyQt4.QtCore import QTime, QTimer
from pyqtgraph.Qt import QtGui, QtCore
import pyqtgraph as pg
from collections import deque
import pytz
UNIX_EPOCH_naive = datetime.datetime(1970, 1, 1, 0, 0) #offset-naive datetime
UNIX_EPOCH_offset_aware = datetime.datetime(1970, 1, 1, 0, 0, tzinfo = pytz.utc) #offset-aware datetime
UNIX_EPOCH = UNIX_EPOCH_naive
TS_MULT_us = 1e6
def now_timestamp(ts_mult=TS_MULT_us, epoch=UNIX_EPOCH):
return(int((datetime.datetime.utcnow() - epoch).total_seconds()*ts_mult))
def int2dt(ts, ts_mult=TS_MULT_us):
return(datetime.datetime.utcfromtimestamp(float(ts)/ts_mult))
def dt2int(dt, ts_mult=TS_MULT_us, epoch=UNIX_EPOCH):
delta = dt - epoch
return(int(delta.total_seconds()*ts_mult))
def td2int(td, ts_mult=TS_MULT_us):
return(int(td.total_seconds()*ts_mult))
def int2td(ts, ts_mult=TS_MULT_us):
return(datetime.timedelta(seconds=float(ts)/ts_mult))
class TimeAxisItem(pg.AxisItem):
def __init__(self, *args, **kwargs):
# super().__init__(*args, **kwargs)
super(TimeAxisItem, self).__init__(*args, **kwargs)
def tickStrings(self, values, scale, spacing):
# PySide's QTime() initialiser fails miserably and dismisses args/kwargs
#return [QTime().addMSecs(value).toString('mm:ss') for value in values]
return [int2dt(value).strftime("%H:%M:%S.%f") for value in values]
class MyApplication(QtGui.QApplication):
def __init__(self, *args, **kwargs):
super(MyApplication, self).__init__(*args, **kwargs)
self.t = QTime()
self.t.start()
maxlen = 200
self.data_x = deque(maxlen=maxlen)
self.data_y = deque(maxlen=maxlen)
self.win = pg.GraphicsWindow(title="Basic plotting examples")
self.win.resize(1000,600)
tai = TimeAxisItem(orientation='bottom')
self.plot = self.win.addPlot(title='Timed data', axisItems={'bottom': TimeAxisItem(orientation='bottom')})
#self.plot.setYRange(0, 150)
self.curve = self.plot.plot()
self.tmr = QTimer()
self.tmr.timeout.connect(self.update)
self.tmr.start(100)
self.y = 100
def update(self):
#self.data.append({'x': self.t.elapsed(), 'y': np.random.randint(0, 100)})
x = now_timestamp()
self.y = self.y + np.random.uniform(-1, 1)
self.data_x.append(x)
self.data_y.append(self.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()
Thanks a lot for these sample…
For my need with only seconds and not utc but France local time, and python 3.6.1 :
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("%Hh%Mmn%Ss") for value in values]
def now_timestamp():
return int(time.time())
def int2dt(ts):
if not ts:
return datetime.datetime.utcfromtimestamp(ts) # workaround fromtimestamp bug (1)
return(datetime.datetime.fromtimestamp(ts))
…
(1) https://github.com/home-assistant/appdaemon/issues/83!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Same code with PyQt4 but without global variables