我正在做一个更大的项目,我需要绘制大量的实时数据。我的程序的一个非常简化的版本是这样的(赞扬一下 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就非常好了。
有什么想法吗?谢谢你的时间!