Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vue.js/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Matplotlib,使用imshow刷新图像速度更快_Python_Matplotlib_Pyqt5_Python 3.6_Imshow - Fatal编程技术网

Python Matplotlib,使用imshow刷新图像速度更快

Python Matplotlib,使用imshow刷新图像速度更快,python,matplotlib,pyqt5,python-3.6,imshow,Python,Matplotlib,Pyqt5,Python 3.6,Imshow,我正在做一个项目,我必须在图形用户界面的窗口上绘制一个320*250像素的图像,如果可能的话,每秒60次。因此,我尝试使用matplotlib 2.0.2、Python 3.6和PyQt5(因为我开始了解这些工具,并使用这些工具处理另一个项目),方法如下: import sys, random, matplotlib from PyQt5 import QtCore, QtGui, QtWidgets matplotlib.use('Qt5Agg') from matplotlib.backe

我正在做一个项目,我必须在图形用户界面的窗口上绘制一个320*250像素的图像,如果可能的话,每秒60次。因此,我尝试使用matplotlib 2.0.2Python 3.6PyQt5(因为我开始了解这些工具,并使用这些工具处理另一个项目),方法如下:

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生成出版物质量的绘图,但不幸的是,它不太适合实时绘图和视频

如果不是严格的要求,考虑使用。它与pyqt5配合良好,旨在弥补matplotlib的不足,特别是在实时领域:

如果您正在做任何需要快速打印更新、视频或实时交互的事情,matplotlib不是最佳选择。 这是(在我看来)matplotlib最大的弱点

(from pyqtgraph site)
它还具有其他(可选)功能,如感兴趣区域、标准化和直方图绘制

此代码可以在我的笔记本电脑上产生约160 FPS(禁用直方图):

import sys, random, matplotlib
from PyQt5 import QtCore, QtGui, QtWidgets

import pyqtgraph as pg
import numpy as np


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.im_widget = pg.ImageView(self)
        # uncomment to hide histogram
        # self.im_widget.ui.histogram.hide()

        self.setLayout(QtWidgets.QVBoxLayout())
        self.layout().addWidget(self.im_widget)

        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
        # numpy random.rand also much faster than list comprehension
        data = np.random.rand(320, 250)
        self.im_widget.setImage(data)

    def initialisationFigure(self):
        self.count = 0
        self.im_widget.show()

    def closeEvent(self, event):
        self.timer.stop()

if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    form = SecondWindow()
    form.show()
    sys.exit(app.exec_())

你看了吗?还没有,我现在回来给你我的新fps.FYI,使用
numpy.random.random((250320))
而不是循环创建图像在我的机器上提高了10-15fps画布的放大也减慢了绘图速度。将窗口的大小从800x600调整到640x480可以让我达到60fpsI,也可以像这样增加fps,但最后,我相信窗口会更大。但谢谢你,我不知道fps的尺寸影响。对于random,这只是一个例子,最后我将通过本地网络收集数据,并在我的计算机上使用LabVIEW程序进行讨论。每秒60帧,因此速度确实更快。但我不知道。将图形作为matplotlib(标签、缩放、光标等)处理容易吗?事实上,这是matplotlib的美观和pyqtgraph的速度之间的折衷。虽然pyqtgraph是高度可配置的,但我未能创建像matplotlib这样漂亮的绘图。至于你的问题,标签、缩放、光标等都是完全可配置的。我建议您安装pyqtgraph并运行其showcase,然后决定:
python-m pyqtgraph.examples