是否有一种方法可以将pyqtgraph上的x刻度更改为字符串(例如时间)并保持自动格式化,以使刻度不会堆积

问题描述 投票:0回答:1

最终目标是将其运行一会儿并暂停一秒钟,并显示最后一个小时(如果可以处理的话),因此n = 3600且t更高。注释掉了第37行(ax.setTicks(dx))后,它绘制得很好,但我想在x轴上显示时间。如果我取消注释该行,则xticks有时间,但是它们不会自动格式化并相互捆绑。 setTickSpacing是解决此问题的正确方法吗?我已经尝试过了,但在这种情况下无法正常工作。

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

x = []
y = []

timelist = []

pw = pg.plot(x, y)

n = 25
i = 0
t = 500

while i < t:
    if i < n:
        xdata = i
        x.append(xdata)

    currentTime = (dt.datetime.now()).strftime("%M:%S")
    timelist.append(currentTime)
    timelist = timelist[-n:]

    ticks = [list(zip(range(n), timelist))]
    ydata = np.random.randint(0,9)
    y.append(ydata)
    y = y[-n:]

    pw.plot(x, y, pen = 'y', clear=True)

    ax = pw.getAxis('bottom')
    dx = [value for value in ticks]
    #ax.setTicks(dx)

    #ax.setTickSpacing(0,0,0)

    pg.QtGui.QApplication.processEvents()

    time.sleep(.01)
    i = i + 1

if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'):
        QtGui.QApplication.instance().exec_()
python pyqt pyqtgraph
1个回答
0
投票

您必须正确构建刻度信息,也不应使用无限循环或sleep(),而应使用QTimer

import datetime as dt
import sys

import numpy as np

from pyqtgraph.Qt import QtGui, QtCore
import pyqtgraph as pg


x, y, timelist = [], [], []

n, i, t = 25, 0, 500

pw = pg.plot(x, y)


def on_timeout():
    global i, x, y, timelist
    if i < n:
        x.append(i)
    i += 1
    ydata = np.random.randint(0, 9)
    y.append(ydata)
    y = y[-n:]

    pw.plot(x, y, pen="y", clear=True)

    currentTime = dt.datetime.now().strftime("%M:%S")
    timelist.append(currentTime)
    timelist = timelist[-n:]

    ticks = list(enumerate(timelist))
    ax = pw.getAxis("bottom")
    ax.setTicks([ticks])


timer = QtCore.QTimer(timeout=on_timeout, interval=100)
timer.start()
pw.win.showMaximized()

if __name__ == "__main__":

    if (sys.flags.interactive != 1) or not hasattr(QtCore, "PYQT_VERSION"):
        QtGui.QApplication.instance().exec_()
© www.soinside.com 2019 - 2024. All rights reserved.