根据先前的答案,pyqtgraph中的图仅接受数值类型的数据,因此您必须对其进行转换,在这种情况下,我们使用数据
timestamp(),然后在自定义中
AxisItem,将其转换为字符串以在
fromtimestamp。
import numpy as npimport pyqtgraph as pgfrom pyqtgraph.Qt import QtCore, QtGuifrom datetime import datetimeclass 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_()


