2017-08-29 21 views
2

GUIのウィンドウで、可能であれば320×250ピクセルの画像を60秒間にプロットする必要があるプロジェクトに取り組んでいます。だから私は、次のように、(私はこれらのツールを知っていて、これを別のプロジェクトで作業を開始するため)PyQt5のPython 3.6 matplotlibの2.0.2でこれをやろうと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_()) 

私は補間をオフにすることができ、図を1回だけ描​​画するように最適化しましたが、このコードでは、プログラムは1秒間に20回しかリフレッシュしませんが、タイマは16ms(1/60Hz)に正しく設定されます。

コードを改善するためのヒントを教えてもらえることを願っています。 私は事前にたくさんありがとう!

+1

あなたは[matplotlibのの 'animation'モジュール](https://matplotlib.org/api/を見たことがありanimation_api.html)? – Paul

+0

まだ、私は今あなたに私の新しいfpsを与えるために戻ってきます。 –

+0

FYIは、画像を作成するループの代わりに 'numpy.random.random((250、320))'を使用して、私のマシンで10-15fpsのブーストを与えます。 – user3419537

答えて

3

Matplotlibは出版品質のプロットを生成しますが、残念ながらリアルタイムのプロットやビデオには適していません。

厳しい要件でない場合は、pyqtgraph moduleを使用することを検討してください。これは、特にリアルタイムエリアで、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_()) 
+0

私のコンピュータ上では60fpsな​​ので、実際には高速です。しかし、私はpyqtgraphを知らない。 matplotlib(ラベル、ズーム、カーソルなど)として図を扱うのは簡単ですか? –

+1

実際、マトプラトリブのプリティさとpyqtgraphの速度との間にはトレードオフがあります。 pyqtgraphは高度に設定可能ですが、私はmatplotlibのような美しいプロットを作成できませんでした。あなたの質問について - ラベル、ズーム、カーソルなどのものは完全に設定可能です。私はあなたがpyqtgraphをインストールし、ショーケースを実行してから、 'python -m pyqtgraph.examples'を決めることをお勧めします。 – 9dogs

関連する問題