【问题标题】:How to I make a PyQtGraph scrolling graph clear the previous line within a loop如何使 PyQtGraph 滚动图清除循环中的上一行
【发布时间】:2022-09-24 02:18:17
【问题描述】:

我希望从具有多列的数组中绘制一些数据,并希望每一列在同一个滚动图上成为不同的线。由于有很多列,我认为将它们绘制在一个循环中是有意义的。我还想用一条线绘制第二个滚动图。

我可以让单线图正确滚动,但包含多条线的图会从更新的数组中重叠,而不会清除前面的线。

如何在 for 循环中清除线条。我认为setData 可能会进行清理。我必须在循环中有pg.QtGui.QApplication.processEvents() 或类似的东西吗?我试图添加该调用,但没有任何效果。

我的代码:

#Based on example from PyQtGraph documentation

import numpy as np
import pyqtgraph as pg

win = pg.GraphicsLayoutWidget(show=True)
win.setWindowTitle(\'pyqtgraph example: Scrolling Plots\')
timer = pg.QtCore.QTimer()

plot_1 = win.addPlot()
plot_2 = win.addPlot()
data1 = np.random.normal(size=(300))
curve1 = plot_1.plot(data1)
data_2d = np.random.normal(size=(3,300))

def update_plot():
    global data1, data_2d
    data1[:-1] = data1[1:]  
    data1[-1] = np.random.normal()
    curve1.setData(data1)

    for idx, n in enumerate(data_2d):
        n[:-1] = n[1:]
        n[-1] = np.random.normal()
        curve2 = plot_2.plot(n,pen=(idx))
        curve2.setData(n)
        #pg.QtGui.QApplication.processEvents() #Does nothing


timer = pg.QtCore.QTimer()
timer.timeout.connect(update_plot)
timer.start(50)

if __name__ == \'__main__\':
    pg.exec()

    标签: python numpy pyqtgraph


    【解决方案1】:

    可以每次使用.clear() 清除所有曲线的图,但这不会非常有效。更好的解决方案是保留所有曲线对象并每次调用setData,就像您使用单曲线图一样。例如。

    curves_2d = [plot_2.plot(pen=idx) for idx, n in enumerate(data_2d)]
    
    # ... in update_plot
            curves_2d[idx].setData(n)
    

    【讨论】:

      猜你喜欢
      • 2016-06-02
      • 2017-02-14
      • 1970-01-01
      • 2022-10-15
      • 1970-01-01
      • 1970-01-01
      • 2022-12-10
      • 2017-09-16
      • 1970-01-01
      相关资源
      最近更新 更多