Skip to content Skip to sidebar Skip to footer

Using Time In Scatter Plot Items X-axis In Pyqt4

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 =

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

enter image description here


Post a Comment for "Using Time In Scatter Plot Items X-axis In Pyqt4"