如何在python pyqtgraph PlotWidget中设置刻度标签

时间:2016-06-28 17:52:52

标签: python-3.x widget pyqt4 pyqtgraph

我试图绘制每天几小时的时间序列,每天之间没有空白时间。如果我使用pg.GraphicsWindow它工作正常。刻度标签是9:00 10:00.17:00。如果我使用pg.PlotWidget或pg.PlotWindow,Alt2和Alt3它不起作用,但显示normla 0.1 0.2 ..代码运行并调用TimeAxisItem类,但x轴刻度标签不会更改。我有一个更大的程序与Qt.QMainWindow()和QtGui.QGridLayout()不接受pg.GraphicsWindow()。我错过了什么?如何在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()

我已经解决了这个问题,这是我的代码。评论欢迎

# -*- 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
    tr=np.arange('2016-06-10 09:00', '2016-06-10 18:00', dtype='datetime64[2h]') # tick labels one day   
    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()

0 个答案:

没有答案