Skip to content

Instantly share code, notes, and snippets.

@friendzis
Created May 7, 2014 11:19
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save friendzis/4e98ebe2cf29c0c2c232 to your computer and use it in GitHub Desktop.
Save friendzis/4e98ebe2cf29c0c2c232 to your computer and use it in GitHub Desktop.
Simple implementation of TimeAxisItem and test program
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_()
@bcag2
Copy link

bcag2 commented Jan 16, 2018

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