我正在开发一个项目,在该项目上我必须在GUI的窗口上绘制320 * 250像素的图像,并且如果可能的话,每秒显示60次。所以,我尝试用matplotlib 2.0.2,Python来做到这一点3.6和PyQt5(因为我开始知道这些工具和与此另一个项目工作),以下列方式:Matplotlib,使用imshow更快地刷新图像
import sys, random, matplotlib
from PyQt5 import QtCore, QtGui, QtWidgets
matplotlib.use('Qt5Agg')
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
import matplotlib.pyplot as plt
class SecondWindow(QtWidgets.QWidget):
def __init__(self, parent=None):
super(SecondWindow, self).__init__(parent)
self.setupUi(self)
def setupUi(self, Form):
Form.setObjectName("Form")
Form.resize(800, 600)
self.figure = plt.figure()
self.canvas = FigureCanvas(self.figure)
self.axes = self.figure.add_subplot(111)
self.setLayout(QtWidgets.QVBoxLayout())
self.layout().addWidget(self.canvas)
self.initialisationFigure()
self.timer = QtCore.QTimer(self)
self.timer.timeout.connect(self.majFigure)
self.timer.start(16)
self.timer2 = QtCore.QTimer(self)
self.timer2.timeout.connect(self.NumberRefreshPerSecond)
self.timer2.start(1000)
def NumberRefreshPerSecond(self):
print(self.count)
self.count = 0
def majFigure(self):
self.count = self.count + 1
self.plot.set_data([[random.random() for x in range(1, 320)] for y in range(1, 250)])
# self.canvas.draw()
self.axes.draw_artist(self.axes.patch)
self.axes.draw_artist(self.plot)
self.canvas.update()
self.canvas.flush_events()
def initialisationFigure(self):
self.plot = self.axes.imshow([[random.random() for x in range(1,320)] for y in range(1,250)], interpolation='none')
self.count = 0
self.canvas.draw()
def closeEvent(self, event):
self.timer.stop()
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
form = SecondWindow()
form.show()
sys.exit(app.exec_())
我进行了优化,就像我可以关闭插值一样,只绘制一次图形,但使用此代码,程序每秒仅刷新数字20次,而计时器正确设置为16ms(1/60Hz)。
我希望有人能帮我给我一些提高我的代码的线索。 我非常感谢你!
你吃过看看[matplotlib的'animation'模块(https://matplotlib.org/api/ animation_api.html)? – Paul
还没有,我现在回来给你我的新fps。 –
仅供参考,使用'numpy.random.random((250,320))'代替循环创建图像,在我的机器上提供了10-15fps的提升 – user3419537