2015-05-15 59 views
1

我檢查了這個版本pyqtgraph的DateAxisItem有時變得不可見

git clone https://github.com/3rdcycle/pyqtgraph.git 
git checkout origin/date-axis-item 
pip uninstall pyqtgraph 
python setup.py install 

我然後運行這個程序。它看起來運行良好,除了我的x軸時間戳進出視圖而沒有我做任何事情。不確定這是該程序中的錯誤還是DateAxisItem。此外,毫秒總是100的倍數。例如,我看到00:00:00:900,00:00:01:200,但從來沒有00:00:00:042?

# -*- coding: utf-8 -*- 
""" 
Created on Thu May 14 21:09:44 2015 

@author: idf 
""" 

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.DateAxisItem): 
    def __init__(self, *args, **kwargs): 
     super(TimeAxisItem, self).__init__(*args, **kwargs) 

    def tickStrings(self, values, scale, spacing): 
     return [QTime().addMSecs(value).toString('hh:mm:ss.zzz') for value in values] 

app = QtGui.QApplication([]) 

win = pg.GraphicsWindow(title="Basic time-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_() 
+0

您正在定義您自己的AxisItem。爲什麼你需要從DateAxisItem繼承? –

+0

因爲我想控制小數秒部分顯示的小數位數。否則,它將長達6位小數。 – Ivan

回答

0

我不知道你的DateAxisItem會發生什麼。如您所知,它尚未合併到PyQtGraph的主要開發分支中。但是,對於特定的應用程序,從頭開始並定義自己的TimeAxisItem可能更容易?如果您從以下代碼開始,將會缺少哪些功能?

class TimeAxisItem(AxisItem): 

    def __init__(self, orientation, **kwargs): 
     super().__init__(orientation, **kwargs) 

    def tickStrings(self, values, scale, spacing): 
     return [self.get_tick(v, spacing) for v in values] 

    def get_tick(self, ts, spacing): 
     dt = datetime.datetime.fromtimestamp(ts) 
     # Here you can decide on the accuracy of the time data 
     # displayed depending on the spacing. 
     if spacing > 60: 
      return "%02d:%02d" % (dt.hour, dt.minute) 
     else: 
      return "%02d:%02d:%02d" % (dt.hour, dt.minute, dt.second)