Estoy usando PyQtGraph '0.9.8+gd627e39' en Python 3.6.2 (32 bits) y Windows 10.
Mi objetivo es trazar datos en tiempo real con un eje X que muestra la fecha y la hora.
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 ...
Busqué problemas relacionados como https://gist.github.com/friendzis/4e98ebe2cf29c0c2c232 , pyqtgraph, plotting time series , pero aún me resulta difícil entender cómo usar DateAxisItem
Traté de hacer un código simple usando el módulo,
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()
pero muestra un mensaje de error y no muestra su eje X en absoluto.
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'
¿Es porque DateAxisItem
no admite fecha y hora? Sería genial si pudiera entender el módulo mirando su código, pero desafortunadamente, mis habilidades no son buenas.
Agradecería si alguien pudiera mostrarme cómo usar el módulo con algunos datos simples.
Basado en una respuesta anterior, la gráfica en pyqtgraph solo acepta datos de tipo numérico, por lo que debe convertirlos y, para este caso, usamos timestamp()
, luego en un AxisItem
personalizado lo convertimos en una cadena para mostrarlo con la ayuda de 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_()