Using time in Scatter plot items x-axis in pyqt4

微笑、不失礼 提交于 2020-01-25 07:37:06

问题


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

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.

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.

Thanks!


回答1:


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



来源:https://stackoverflow.com/questions/48887163/using-time-in-scatter-plot-items-x-axis-in-pyqt4

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!