Created
May 25, 2017 17:52
-
-
Save iverasp/9349dffa42aeffb32e48a0868edfa32d to your computer and use it in GitHub Desktop.
PyQtGraph datetime axis in PyQt5
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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']) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Is it normal when I run the script nothing is displayed ?