I am trying to plot a time series of a few hours per day date without blank time between each day. It works fine if I use pg.GraphicsWindow.The tick labels are 9:00 10:00 ..17:00. If I use pg.PlotWidget or pg.PlotWindow, Alt2 and Alt3 it does not work but the normla 0.1 0.2 ..is displayed. The code runs and the TimeAxisItem class is called but the x-axis tick labels do not change. I have a larger program with Qt.QMainWindow() and QtGui.QGridLayout() which does not accept pg.GraphicsWindow(). What am I missing??. How can you set tick labels in a PlotWidget?
# -*- coding: utf-8 -*-
''' Setting x-axis labels for time series
'''
import datetime as dt
from pyqtgraph.Qt import QtGui, QtCore
import numpy as np
import pyqtgraph as pg
class TimeAxisItem(pg.AxisItem):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
tr=np.arange('2016-06-10 09:00', '2016-06-10 18:00', dtype='datetime64[h]')
tnorm=(tr-tr[0])/(tr[-1]-tr[0]) #Map time to 0.0-1.0
ttick=list()
for i,t in enumerate(tr):
tstr=np.datetime64(t).astype(dt.datetime)
ttick.append( (tnorm[i], tstr.strftime("%H:%M")) )
self.setTicks([ttick])
def main():
app = QtGui.QApplication([])
x=np.arange(0.0, 1.0, 0.02)
y=np.sin(2*np.pi*x)
#Alt 1
win = pg.GraphicsWindow(title="Basic plotting")
plot=win.addPlot(title='Timed data', axisItems={'bottom': TimeAxisItem(orientation='bottom')})
plot.plot(x,y)
# Alt 2
#win = pg.PlotWidget(title="Basic plotting")
#win.plot(title='Timed data', axisItems={'bottom': TimeAxisItem(orientation='bottom')})
#win.plot(x,y)
#Alt 3
#win=pg.PlotWindow(title="Basic plotting")
#win.plot(title='Timed data', axisItems={'bottom': TimeAxisItem(orientation='bottom')})
#win.plot(x,y)
win.show()
app.exec_()
if __name__ == '__main__':
main()