Skip to content

Instantly share code, notes, and snippets.

@iverasp
Created May 25, 2017 17:52
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iverasp/9349dffa42aeffb32e48a0868edfa32d to your computer and use it in GitHub Desktop.
Save iverasp/9349dffa42aeffb32e48a0868edfa32d to your computer and use it in GitHub Desktop.
PyQtGraph datetime axis in PyQt5
from PyQt5.QtWidgets import QWidget, QGridLayout
import pyqtgraph as pg
from utils import TimeAxisItem, timestamp
class ExampleWidget(QWidget):
def __init__(self, parent=None):
QWidget.__init__(self, parent)
self.plot = pg.PlotWidget(
title="Example plot",
labels={'left': 'Reading / mV'},
axisItems={'bottom': TimeAxisItem(orientation='bottom')}
)
self.plot.setYRange(0, 5000)
self.plot.setXRange(timestamp(), timestamp() + 100)
self.plot.showGrid(x=True, y=True)
self.layout = QGridLayout(self)
self.layout.addWidget(self.plot, 0, 0)
self.plotCurve = self.plot.plot(pen='y')
self.plotData = {'x': [], 'y': []}
def updatePlot(self, newValue):
self.plotData['y'].append(newValue)
self.plotData['x'].append(timestamp())
self.plotCurve.setData(self.plotData['x'], self.plotData['y'])
import pyqtgraph as pg
import datetime
import time
def timestamp():
return int(time.mktime(datetime.datetime.now().timetuple()))
class TimeAxisItem(pg.AxisItem):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.setLabel(text='Time', units=None)
self.enableAutoSIPrefix(False)
def tickStrings(self, values, scale, spacing):
return [datetime.datetime.fromtimestamp(value).strftime("%H:%M") for value in values]
@Shamzy13
Copy link

Shamzy13 commented Apr 4, 2023

Is it normal when I run the script nothing is displayed ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment