gpt4 book ai didi

python - 如何使用 PyQtGraph 的 DateAxisItem?

转载 作者:行者123 更新时间:2023-11-28 17:08:58 24 4
gpt4 key购买 nike

我在 Python 3.6.2(32 位)和 Windows 10 上使用 PyQtGraph '0.9.8+gd627e39'。

我的目标是用显示日期时间的 X 轴绘制实时数据。

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
...

我查了相关的问题比如 https://gist.github.com/friendzis/4e98ebe2cf29c0c2c232 , pyqtgraph, plotting time series , 但我仍然很难掌握如何使用 DateAxisItem

我尝试使用该模块制作一个简单的代码,

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()

但它显示一条错误消息并且根本不显示其 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不支持datetime?如果我能通过查看它的代码来理解该模块,那就太好了,但不幸的是,我的技能并不好。

如果有人能告诉我如何使用带有一些简单数据的模块,我将不胜感激。

最佳答案

基于之前的answer ,pyqtgraph 中的绘图只接受数字类型的数据,因此您必须将其转换,对于这种情况,我们使用 timestamp(),然后在自定义 AxisItem 中,我们将其转换为字符串在 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

关于python - 如何使用 PyQtGraph 的 DateAxisItem?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49046931/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com