8

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()
Joe
  • 575
  • 6
  • 24
Ulf Wållgren
  • 161
  • 2
  • 8

1 Answers1

0
# -*- coding: utf-8 -*-
''' Setting x-axis labels for time series 
Window, pyqtgraph (09.10) numpy (1.11.1) PyQt4(4.11.4)
'''

import datetime as dt
from pyqtgraph.Qt import QtGui, QtCore
import numpy as np
import pyqtgraph as pg

def main():
    app = QtGui.QApplication([])
    #Plot some data for 2 days
    x=np.arange(0.0, 1.0, 0.02)
    day0=100*np.sin(2*np.pi*x)   # Just som data to plot
    day1=100*(np.cos(2*np.pi*x)-1)   # Just som data to plot
    xx=np.concatenate([x,x+1]) # two days
    yy=np.concatenate([day0,day1])
    
    win = pg.PlotWidget(title="Plotting time series")            
    win.resize(1600,400)    
    win.plot(xx,yy)

    # Tick labels
    # tick labels one day        
    tr=np.arange('2016-06-10 09:00', '2016-06-10 18:00', dtype='datetime64[2h]')  
    tday0=(tr-tr[0])/(tr[-1]-tr[0])  #Map time to 0.0-1.0 day 2 1.0-2.0 ...
    tday1=tday0+1
    tnorm=np.concatenate([tday0,tday1])
    tr[-1]=tr[0]  # End day=start next day
    # Create tick labels for axis.setTicks
    ttick=list()    
    for i,t in enumerate(np.concatenate([tr,tr])):
        tstr=np.datetime64(t).astype(dt.datetime)
        ttick.append(  (tnorm[i],  tstr.strftime("%H:%M")))  

    ax=win.getAxis('bottom')    #This is the trick  
    ax.setTicks([ttick])

    # Set grid x and y-axis
    ax.setGrid(255)
    ay=win.getAxis('left')
    ay.setGrid(255)

    win.show()
    app.exec_()

if __name__ == '__main__':
    main()
Joe
  • 575
  • 6
  • 24
  • 1
    You should try to improve the answer by showing what exactly was changed or at least giving the context of this being the fixed code that was in the question. – duckboycool Aug 10 '22 at 17:27