Matplotlib生成出版物质量的绘图,但不幸的是,它不太适合实时绘图和视频。
pyqtgraph
module
. 它与pyqt5配合良好,旨在弥补
matplotlib
如果您正在做任何需要快速打印更新、视频或实时交互的事情,matplotlib不是最佳选择。
(from pyqtgraph site)
它还具有其他(可选)功能,如感兴趣区域、归一化和直方图绘制。
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_())