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_() |
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
fix to work with Python 2.7.12