2016-09-23 106 views

回答

1

你需要一个QwtScaleDraw类例如为:

class TimeScaleDraw(QwtScaleDraw): 
    def __init__(self, baseTime, *args): 
     QwtScaleDraw.__init__(self, *args) 
     self.baseTime = baseTime 

    def label(self, value): 
     upTime = self.baseTime.addSecs(int(value)) 
     return QwtText(upTime.toString()) 

那么您修改x轴与此代码:

self.setAxisScaleDraw(
     QwtPlot.xBottom, TimeScaleDraw(self.cpuStat.upTime())) 

其中自我的基类是QwtPlot。 cpuStat.upTime()返回一个QTime实例。值代表您的时间数据点之一。这段代码来自cpuDemo示例代码。

得到这个例子cpuDemo使用python xy 2.7。对进口模块进行以下更改:

import os 
import sys 
import numpy as np 

from PyQt4.QtGui import (QApplication, QColor, QBrush, QWidget, QVBoxLayout, 
         QLabel) 
from PyQt4.QtCore import QRect, QTime 
from PyQt4.QtCore import Qt 
from PyQt4.Qwt5 import (QwtPlot, QwtPlotMarker, QwtScaleDraw, QwtLegend, QwtPlotCurve, 
      QwtPlotItem, QwtText)#, QwtLegendData 

然后注释掉所有图例行。

我采用了这段代码来使用QDateTime类。这里是一个片段:

from PyQt4 import QtCore, QtGui, Qt 
from PyQt4 import Qwt5 

class TimeScaleDraw(Qwt5.QwtScaleDraw): 
    def __init__(self, *args): 
     Qwt5.QwtScaleDraw.__init__(self,unixBaseTime *args) 
     self.basetime=unixBaseTime 
     self.fmt='h:mm\nd-MMM-yyyy' 

    def label(self, value): 
     dt = QtCore.QDateTime.fromMSecsSinceEpoch((value+self.basetime)) 
     return Qwt5.QwtText(dt.toString(self.fmt)) 

... 

class Ui_TabWidget(object): 
    def setupUi(self, TabWidget): 
     self.qwtPlot = Qwt5.QwtPlot() 
     self.qwtPlot.setAxisScaleDraw(Qwt5.QwtPlot.xBottom, TimeScaleDraw(unixBaseTime)) 

欣赏漂亮的图片