在 pyqt4 中使用散点图项 x 轴中的时间 [英] Using time in Scatter plot items x-axis in pyqt4

查看:72
本文介绍了在 pyqt4 中使用散点图项 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 轴中的值.但我不确定我到底需要如何处理这个问题.

I hope it should be definetely possible to overwrite the values in x-axis. But i am not sure how exactly i need to handle this one.

我无法直接在 x 轴上绘制时间,所以我尝试将 in 转换为 int 值,一旦绘制完成,我需要用提供的值替换它的 x 值.

I couldn't directly plot time in x-axis, so i tried to convert in into int values and once plotting is done i need to replace its x-values with the provided values.

谢谢!

推荐答案

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

The string must be converted to QTime, and from QTime to int, where that integer is the time in seconds elapsed since the beginning of the day, then the AxisItem is established when creating the Plot:

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_()

这篇关于在 pyqt4 中使用散点图项 x 轴中的时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆