在pyqt4中的Scatter绘图项x轴中使用时间

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在pyqt4中的Scatter绘图项x轴中使用时间相关的知识,希望对你有一定的参考价值。

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

app = QtGui.QApplication(sys.argv)
mw = QtGui.QMainWindow()
mw.resize(800, 800)
view = pg.GraphicsLayoutWidget()
mw.setCentralWidget(view)
mw.setWindowTitle('pyqtgraph example: ScatterPlot')
w1 = view.addPlot()
x = ['0:09:48', '0:09:49', '0:09:50', '0:09:51', '0:09:52', '0:09:53', '0:09:54', '0:09:55', '0:09:56', '0:09:57']
x1=[583, 584, 585, 586, 587, 588, 589, 590, 591, 592]
y = [10, 8, 6, 4, 2, 20, 18, 16, 14, 12]
import datetime
import pytz
UNIX_EPOCH_naive = datetime.datetime(1970, 1, 1, 0, 0) #offset-naive datetime
UNIX_EPOCH_offset_aware = datetime.datetime(1970, 1, 1, 0, 0, tzinfo = pytz.utc) #offset-aware datetime
UNIX_EPOCH = UNIX_EPOCH_naive

TS_MULT_us = 1e6

def now_timestamp(ts_mult=TS_MULT_us, epoch=UNIX_EPOCH):
    print (int((datetime.datetime.utcnow() - epoch).total_seconds()*ts_mult))
    return(int((datetime.datetime.utcnow() - epoch).total_seconds()*ts_mult))

def int2dt(ts, ts_mult=TS_MULT_us):
    return(datetime.datetime.utcfromtimestamp(float(ts)/ts_mult))

def dt2int(dt, ts_mult=TS_MULT_us, epoch=UNIX_EPOCH):
    delta = dt - epoch
    return(int(delta.total_seconds()*ts_mult))

def td2int(td, ts_mult=TS_MULT_us):
    return(int(td.total_seconds()*ts_mult))

def int2td(ts, ts_mult=TS_MULT_us):
    return(datetime.timedelta(seconds=float(ts)/ts_mult))

class TimeAxisItem(pg.AxisItem):
    def __init__(self, *args, **kwargs):
        super(TimeAxisItem, self).__init__(*args, **kwargs)

    def tickStrings(self, values, scale, spacing):
        # print values
        # print [int2dt(value).strftime("%H:%M:%S") for value in values]
        return [int2dt(value).strftime("%H:%M:%S") for value in values]

# Create seed for the random
time = QtCore.QTime.currentTime()
QtCore.qsrand(time.msec())

for i in range(len(x)):
    s = pg.ScatterPlotItem([x1[i]], [y[i]], size=10, pen=pg.mkPen(None),axisItems={'bottom':  x[i]})  # brush=pg.mkBrush(255, 255, 255, 120))
    print s
    s.setBrush(QtGui.QBrush(QtGui.QColor(QtCore.qrand() % 256, QtCore.qrand() % 256, QtCore.qrand() % 256)))
    w1.addItem(s,axisItems={'bottom':  ['0:09:48', '0:09:49', '0:09:50', '0:09:51', '0:09:52', '0:09:53', '0:09:54', '0:09:55', '0:09:56', '0:09:57']})
mw.show()
sys.exit(QtGui.QApplication.exec_())

我希望它可以定义为覆盖x轴中的值。但我不确定我究竟需要处理这个问题。

我不能直接在x轴上绘制时间,所以我试图将其转换为int值,一旦完成绘图,我需要用提供的值替换它的x值。

谢谢!

答案

string必须转换为QTime,从QTime转换为int,其中该整数是从一天开始以来经过的秒数,然后在创建Plot时建立AxisItem

import sys
from pyqtgraph.Qt import QtGui, QtCore
import pyqtgraph as pg


class TimeAxisItem(pg.AxisItem):
    def tickStrings(self, values, scale, spacing):
        return [QtCore.QTime(0, 0, 0).addSecs(value).toString() for value in values]

app = QtGui.QApplication(sys.argv)
mw = QtGui.QMainWindow()
mw.resize(800, 800)
view = pg.GraphicsLayoutWidget()
mw.setCentralWidget(view)
mw.setWindowTitle('pyqtgraph example: ScatterPlot')
stringaxis = TimeAxisItem(orientation='bottom')
w1 = view.addPlot(axisItems={'bottom': stringaxis})

x = ['0:09:48', '0:09:49', '0:09:50', '0:09:51', '0:09:52', '0:09:53', '0:09:54', '0:09:55', '0:09:56', '0:09:57']
y = [10, 8, 6, 4, 2, 20, 18, 16, 14, 12]


time = QtCore.QTime.currentTime()
QtCore.qsrand(time.msec())

for xi, yi in zip(x, y):
    s = pg.ScatterPlotItem([QtCore.QTime(0, 0, 0).secsTo(QtCore.QTime.fromString(xi, "h:mm:ss"))], [yi], size=10, pen=pg.mkPen(None))
    s.setBrush(QtGui.QBrush(QtGui.QColor(QtCore.qrand() % 256, QtCore.qrand() % 256, QtCore.qrand() % 256)))
    w1.addItem(s)

mw.show()

if __name__ == '__main__':
    import sys
    if sys.flags.interactive != 1 or not hasattr(QtCore, 'PYQT_VERSION'):
        pg.QtGui.QApplication.exec_()

enter image description here

以上是关于在pyqt4中的Scatter绘图项x轴中使用时间的主要内容,如果未能解决你的问题,请参考以下文章

在绘图直方图 x 轴中格式化刻度

在 PyQt4 #2 中使用 PyQtGraph 进行实时绘图

从 D3.js 轴中删除结束标记

在recycleview时间轴中为每个视图添加drawable

vtk 6.x,Qt:3D(线、面、散点图)绘图

如何更新第二个绘图轴 plotly Scatter3D subplots