Я не могу на всю жизнь придумать лучший способ построения графика в реальном времени с помощью matplotlib внутри pyqt5.
Как сейчас, он работает, но при переопределении matplotlib наблюдается заметное заикание / отставание aws (если вы перетащите окно вокруг, вы обязательно увидите его).
Каков стандартный способ сделать это, и как далеко я?
from PyQt5.QtWidgets import*
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg,NavigationToolbar2QT
from matplotlib.figure import Figure
import time
import random
from PyQt5 import QtCore
from PyQt5 import QtWidgets
from PyQt5.QtCore import pyqtSignal
import finalmtplwindow
import sys
class Mainwindow(QtWidgets.QMainWindow, finalmtplwindow.Ui_MainWindow):
def __init__(self,parent=None):
super(Mainwindow, self).__init__(parent)
self.setupUi(self)
self.showvalues = Threading2()
self.showvalues.my_signal2.connect(self.write)
self.showvalues.start()
def write(self,val):
self.progressBar.setValue(int(val[0]))
self.radioButton.setChecked(bool(val[1]))
class Threading2(QtCore.QThread):
my_signal2=pyqtSignal(list)
def __init__(self,myvar=0,parent=None):
super(Threading2, self).__init__(parent)
self.myvar=myvar
def run(self):
while 1:
self.my_signal2.emit([random.randint(0,100),random.choice([1,0])])
time.sleep(0.1)
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(792, 562)
MainWindow.setStyleSheet("background-color: rgb(98, 98, 98);")
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.progressBar = QtWidgets.QProgressBar(self.centralwidget)
self.progressBar.setGeometry(QtCore.QRect(40, 40, 118, 23))
self.progressBar.setProperty("value", 24)
self.progressBar.setAlignment(QtCore.Qt.AlignRight|QtCore.Qt.AlignTrailing|QtCore.Qt.AlignVCenter)
self.progressBar.setTextVisible(True)
self.progressBar.setObjectName("progressBar")
self.radioButton = QtWidgets.QCheckBox(self.centralwidget)
self.radioButton.setGeometry(QtCore.QRect(620, 50, 81, 16))
self.radioButton.setIconSize(QtCore.QSize(20, 20))
self.radioButton.setCheckable(True)
self.radioButton.setObjectName("radioButton")
self.b = QtWidgets.QCheckBox(self.centralwidget)
self.b.setGeometry(QtCore.QRect(620, 70, 81, 16))
self.b.setIconSize(QtCore.QSize(20, 20))
self.b.setCheckable(True)
self.b.setObjectName("radioButton")
self.MplWidget = MplWidget(self.centralwidget)
self.MplWidget.setGeometry(QtCore.QRect(20, 140, 751, 391))
self.MplWidget.setObjectName("MplWidget")
MainWindow.setCentralWidget(self.centralwidget)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.textbox = QtWidgets.QLineEdit(self.centralwidget)
self.textbox.setGeometry(QtCore.QRect(300, 40, 40, 40))
self.textbox.setText('0')
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
self.radioButton.toggled.connect(self.update2)
self.b.toggled.connect(self.reset)
self.data = [[0], [0]]
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.radioButton.setText(_translate("MainWindow", "Generate"))
self.b.setText(_translate("MainWindow", "Reset"))
def reset(self):
self.data=[[0],[0]]
self.MplWidget.canvas.axes.clear()
self.MplWidget.canvas.axes.patch.set_alpha(0.5)
self.MplWidget.canvas.draw()
def update2(self):
self.data[0].append(random.choice([x for x in range(10)]))
self.data[1].append(self.data[1][-1]+1)
if not self.textbox.text():
size=0
else:
size=int(self.textbox.text())*-1
self.MplWidget.canvas.axes.clear()
self.MplWidget.canvas.axes.plot(self.data[1][size:], self.data[0][size:])
self.MplWidget.canvas.axes.legend('sig', loc='best')
self.MplWidget.canvas.axes.set_title('random signal generator')
self.MplWidget.canvas.axes.patch.set_alpha(0.5)
self.MplWidget.canvas.draw()
self.MplWidget.canvas.flush_events()
class MplWidget(QWidget):
def __init__(self, parent=None):
QWidget.__init__(self, parent)
self.figure = Figure()
self.canvas = FigureCanvasQTAgg(self.figure)
self.figure.set_facecolor("none") # making it transparent
self.setStyleSheet("background-color:transparent;")
self.figure.tight_layout()
self.toolbar = NavigationToolbar2QT(self.canvas, self)
vertical_layout = QVBoxLayout()
vertical_layout.addWidget(self.canvas)
vertical_layout.addWidget(self.toolbar)
self.canvas.axes = self.canvas.figure.add_subplot(111)
self.canvas.axes.patch.set_alpha(0.5)
self.canvas.axes.set_facecolor('none')
self.setLayout(vertical_layout)
if __name__ == "__main__":
a = QtWidgets.QApplication(sys.argv)
app=Mainwindow()
app.show()
a.exec_()