绘图速度的提高

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

我正在做一个更大的项目,需要绘制大量实时数据。这是我程序的非常简化的版本(对https://stackoverflow.com/a/41687202/1482066表示感谢:

from PyQt5 import QtCore, QtGui, QtWidgets
import pyqtgraph as pg
import random
import time

number_of_plots = 6
number_of_caps = 48
show_plots = True

print(f"Number of plots: {number_of_plots}")
print(f"Number of caps: {number_of_caps}")


class MainWindow(QtWidgets.QMainWindow):
    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)
        self.central_widget = QtWidgets.QStackedWidget()
        self.setCentralWidget(self.central_widget)
        self.login_widget = LoginWidget(self)
        self.login_widget.button.clicked.connect(self.plotter)
        self.central_widget.addWidget(self.login_widget)
        self.data = dict()
        self.curve = dict()

        self.points_kwargs = list()

        colors = ["#000000", "#e6194B", "#f58231", "#3cb44b", "#42d4f4", "#4363d8", "#911eb4", "#f032e6", "#bfef45" ,"#000075", "#e6beff", "#9A6324"]

        self.colors = colors * 4

        for color in self.colors:
            self.points_kwargs.append({"pen": None,
                                       "symbol": 'x',
                                       "symbolSize": 8,
                                       "symbolPen": color,
                                       "symbolBrush": color})

    def plotter(self):
        for j in range(number_of_plots):
            self.data[j] = list()
            self.curve[j] = list()
            for i in range(number_of_caps):
                self.data[j].append([i])
                self.curve[j].append(self.login_widget.plots[j].getPlotItem().plot(**self.points_kwargs[i]))

        self.timer = QtCore.QTimer()
        self.timer.timeout.connect(self.updater)
        self.timer.start(0)

    def updater(self):
        starttime = time.perf_counter()

        for key in self.data.keys():
            for i in range(number_of_caps):
                self.data[key][i].append(self.data[key][i][-1]+0.2*(0.5-random.random()))
                self.curve[key][i].setData(self.data[key][i])

        print(f"Plottime: {time.perf_counter() - starttime}")


class LoginWidget(QtWidgets.QWidget):
    def __init__(self, parent=None):
        super(LoginWidget, self).__init__(parent)
        layout = QtWidgets.QHBoxLayout()
        self.button = QtWidgets.QPushButton('Start Plotting')
        layout.addWidget(self.button)

        self.plots = list()

        for i in range(number_of_plots):
            plot = pg.PlotWidget()
            self.plots.append(plot)
            layout.addWidget(plot)
            if not show_plots:
                plot.hide()

        self.plots[0].show()
        self.setLayout(layout)


if __name__ == '__main__':
    app = QtWidgets.QApplication([])
    window = MainWindow()
    window.show()
    app.exec_()

顶部的数字显示了它如何随情节和上限而缩放。 6和48是我的程序需要处理的原因。运行该程序,绘图时间约为1秒/更新。至少在我的机器上。

为了使复杂的事情变得容易:我需要尽可能减少绘制时间。因子2可能还可以,10会很好。

有什么想法吗?谢谢您的时间!

最佳

python python-3.x pyqt pyqt5 pyqtgraph
1个回答
0
投票

我想出了一种提高速度的方法。如我的评论中所述,如果缩小,绘制速度会更快。图的重定比例实际上占用了图所需的大部分时间。在我的情况下,绘制一个点大约需要2-4ms,而重新调整比例又需要20-40ms。

我通过计算绘图的最大x和y范围并在绘图开始之前设置范围来解决此问题。如果您不知道会显示多少数据,这将无济于事,但是在这种情况下,自动潘功能可能会有所帮助。我在代码中添加了以下内容:

plot.setXRange(min_x,max_x, 0.05)
plot.setYRange(min_y,max_y, 0.05)
plot.hideButtons()

。hideButtons()禁用绘图左下角的自动缩放按钮。这样可以防止用户重新启用自动缩放。用户仍然可以根据需要放大和缩小,但是不再会自动缩放。这不是一个完美的解决方案,但对我而言有效。

© www.soinside.com 2019 - 2024. All rights reserved.