How Can I Use Dateaxisitem Of Pyqtgraph?
I'm using PyQtGraph '0.9.8+gd627e39' on Python 3.6.2(32bit) and Windows 10. My goal is to plot real time data with an X-axis that shows datetime. Time
Solution 1:
Based on a previous answer, the plot in pyqtgraph only accept data of numerical type so you must convert it and for this case we use timestamp()
, then in a custom AxisItem
we convert it to string to show it with the help of fromtimestamp
.
import numpy as np
import pyqtgraph as pg
from pyqtgraph.Qt import QtCore, QtGui
from datetime import datetime
classTimeAxisItem(pg.AxisItem):
deftickStrings(self, values, scale, spacing):
return [datetime.fromtimestamp(value) for value in values]
list_x = [datetime(2018, 3, 1, 9, 36, 50, 136415),
datetime(2018, 3, 1, 9, 36, 51, 330912),
datetime(2018, 3, 1, 9, 36, 51, 382815),
datetime(2018, 3, 1, 9, 36, 52, 928818)]
list_y = [10, 9, 12, 11]
app = QtGui.QApplication([])
date_axis = TimeAxisItem(orientation='bottom')
graph = pg.PlotWidget(axisItems = {'bottom': date_axis})
graph.plot(x=[x.timestamp() for x in list_x], y=list_y, pen=None, symbol='o')
graph.show()
if __name__ == '__main__':
import sys
if (sys.flags.interactive != 1) ornothasattr(QtCore, 'PYQT_VERSION'):
QtGui.QApplication.instance().exec_()
Post a Comment for "How Can I Use Dateaxisitem Of Pyqtgraph?"