如何使用PyQtGraph的DateAxisItem?
我在Python 3.6.2(32bit)和Windows 10上使用PyQtGraph‘0.9.8 + gd627e39’
我的目标是使用显示日期时间的X轴绘制实时数据。
Time Valuedatetime.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
...
我查找了相关问题,例如
https://gist.github.com/friendzis/4e98ebe2cf29c0c2c232,pyqtgraph,绘制时间序列,但是我仍然很难掌握如何使用DateAxisItem
我尝试使用该模块编写简单的代码,
import numpy as npimport 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()
但它显示错误消息,并且根本不显示其X轴。
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'
是因为DateAxisItem
不支持日期时间吗?如果可以通过看一下模块的代码来理解该模块,那就太好了,但是不幸的是,我的技能并不好。
如果有人可以向我展示如何将模块与一些简单数据一起使用,我将不胜感激。
回答:
根据先前的答案,pyqtgraph中的图仅接受数值类型的数据,因此您必须对其进行转换,在这种情况下,我们使用数据timestamp()
,然后在自定义中AxisItem
,将其转换为字符串以在fromtimestamp
。
import numpy as npimport 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_()
以上是 如何使用PyQtGraph的DateAxisItem? 的全部内容, 来源链接: utcz.com/qa/414431.html