Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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                                                Value
datetime.datetime(2018, 3, 1, 9, 36, 50, 136415)    10
datetime.datetime(2018, 3, 1, 9, 36, 51, 330912)    9
datetime.datetime(2018, 3, 1, 9, 36, 51, 382815)    12
datetime.datetime(2018, 3, 1, 9, 36, 52, 928818)    11
...

I looked up related issues such as https://gist.github.com/friendzis/4e98ebe2cf29c0c2c232, pyqtgraph, plotting time series, but it's still hard for me to grasp how to use DateAxisItem

I tried to make a simple code using the module,

import numpy as np
import pyqtgraph as pg
from pyqtgraph.Qt import QtCore, QtGui
from datetime import datetime
from time import time

t1 = datetime.now()
t2 = datetime.now()

list_x = [ t1, t2 ]
list_y = [ 0, 1 ]

date_axis = pg.graphicsItems.DateAxisItem.DateAxisItem(orientation = 'bottom')
graph = pg.PlotWidget(axisItems = {'bottom': date_axis})

graph.plot(x=list_x, y=list_y, pen=None, symbol='o')
graph.show()

but it shows an error message and doesn't show its X-axis at all.

Traceback (most recent call last):
File "<tmp 10>", line 19, in <module>
    graph.plot(x=list_x, y=list_y, pen=None, symbol='o')
File "d:\python36-32\lib\site-packages\pyqtgraph\graphicsItems\PlotItem\PlotItem.py", line 636, in plot
    item = PlotDataItem(*args, **kargs)
File "d:\python36-32\lib\site-packages\pyqtgraph\graphicsItems\PlotDataItem.py", line 177, in __init__
    self.setData(*args, **kargs)
File "d:\python36-32\lib\site-packages\pyqtgraph\graphicsItems\PlotDataItem.py", line 461, in setData
    self.updateItems()
File "d:\python36-32\lib\site-packages\pyqtgraph\graphicsItems\PlotDataItem.py", line 493, in updateItems
    self.scatter.setData(x=x, y=y, **scatterArgs)
File "d:\python36-32\lib\site-packages\pyqtgraph\graphicsItems\ScatterPlotItem.py", line 308, in setData
    self.addPoints(*args, **kargs)
File "d:\python36-32\lib\site-packages\pyqtgraph\graphicsItems\ScatterPlotItem.py", line 388, in addPoints
    newData['x'] = kargs['x']
TypeError: float() argument must be a string or a number, not 'datetime.datetime'

Is it because DateAxisItem doesn't support datetime? It would be great if I could understand the module by looking its code, but unfortunately, my skills are not good.

I'd appreciate it if anyone could show me how to use the module with some simple data.

like image 219
maynull Avatar asked Mar 01 '18 09:03

maynull


1 Answers

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


class TimeAxisItem(pg.AxisItem):
    def tickStrings(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) or not hasattr(QtCore, 'PYQT_VERSION'):
        QtGui.QApplication.instance().exec_()

enter image description here

like image 78
eyllanesc Avatar answered Nov 06 '22 20:11

eyllanesc