Skip to content

Instantly share code, notes, and snippets.

@pyRobShrk
Created November 30, 2017 19:30
Show Gist options
  • Save pyRobShrk/641714fe40d632940f02f825deaa5819 to your computer and use it in GitHub Desktop.
Save pyRobShrk/641714fe40d632940f02f825deaa5819 to your computer and use it in GitHub Desktop.
pyqtgraph dateAxis
# dateAxis.py
# Usage: save dateAxis.py in directory with pyqtgraph py file
# Use sample code below to create plot with date axis
# from dateAxis import *
# daxis = DateAxis(orientation='bottom')
# flo = pg.PlotWidget(axisItems={'bottom': daxis, 'left': yaxis})
import time
import pyqtgraph as pg
import numpy as np
from pyqtgraph.Qt import QtGui, QtCore
def reduce(li, length):
if len(li) <= length:
return li
r = int(len(li)/round(length,0))
a = []
i = 1
for b in li:
if i%r==0:
a.append(b)
i+=1
return a
#override AxisItem class to handle dates
class DateAxis(pg.AxisItem):
def tickStrings(self, values, scale, spacing):
strns = []
#if rng < 120:
# return pg.AxisItem.tickStrings(self, values, scale, spacing)
if spacing < 3600*24: #less than 1 day
string = '%H:%M'
elif spacing >= 3600*24 and spacing < 3600*24*30: #1 day to 30 days
string = "%d%b'%y"
elif spacing >= 3600*24*30 and spacing < 3600*24*30*12: #30 days to 1 years
string = "%b'%y"
elif spacing >=3600*24*30*12: #more than 1 years
string = '%Y'
for x in values:
try:
strns.append(time.strftime(string, time.localtime(x)))
except ValueError: ## Windows can't handle dates before 1970
strns.append('')
return strns
def tickValues(self, minVal, maxVal, size):
'''
Return the values and spacing of ticks to draw::
[
(spacing, [major ticks]),
(spacing, [minor ticks]),
...
]
'''
minVal, maxVal = sorted((minVal, maxVal))
span = maxVal - minVal
minorTick = []
if span < 3600*24*3: #less than 3 days
minVal = round(minVal/900,0)*900 #900s = 15min
maxVal = round(maxVal/900,0)*900
for t in np.arange(minVal,maxVal,900):
ti = time.localtime(t)
if ti.tm_min == 0:
minorTick.append(t)
mnSp = 3600
elif span >= 3600*24*3 and span < 3600*24*30*6: #3 days to 6 months
minVal = round(minVal/(3600),0)*3600 #3600s = 1 hour
maxVal = round(maxVal/(3600),0)*3600
for t in np.arange(minVal,maxVal,3600):
ti = time.localtime(t)
if ti.tm_hour == 0:
minorTick.append(t)
mnSp = 3600*24
elif span >= 3600*24*30*6 and span < 3600*24*30*36: #> 6 months to 3 years
minVal = round(minVal/(3600*24),0)*3600*24 #3600*24s = 1 day
maxVal = round(maxVal/(3600*24),0)*3600*24
for t in np.arange(minVal,maxVal,3600*24):
ti = time.localtime(t)
if ti.tm_mday == 1:
minorTick.append(t)
mnSp = 3600*24*30
elif span >= 3600*24*30*36: #over three years
minVal = round(minVal/(3600*24),0)*3600*24 #3600*24s = 1 day
maxVal = round(maxVal/(3600*24),0)*3600*24
for t in np.arange(minVal,maxVal,3600*24):
ti = time.localtime(t)
if ti.tm_yday == 1:
minorTick.append(t)
mnSp = 3600*24*30
minorTick = reduce(minorTick,12)
if len(minorTick)>1:
mnSp = minorTick[1]-minorTick[0]
return [(mnSp,minorTick)]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment