pip install pyqtgraph
操作步骤如下:
# test1.py
from PyQt5 import QtCore, QtGui, QtWidgets
from pyqtgraph import PlotWidget
class Ui_Form(object):
def setupUi(self, Form):
Form.setObjectName("Form")
Form.resize(965, 783)
self.plotWidget = PlotWidget(Form)
self.plotWidget.setGeometry(QtCore.QRect(60, 100, 851, 521))
self.plotWidget.setStyleSheet("")
self.plotWidget.setObjectName("plotWidget")
self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
_translate = QtCore.QCoreApplication.translate
Form.setWindowTitle(_translate("Form", "Form"))
from PyQt5.QtWidgets import QWidget, QMainWindow, QApplication
from test1 import Ui_Form
from constant import x, y, b
class TestUI(QWidget, Ui_Form):
def __init__(self):
super(TestUI, self).__init__()
self.setupUi(self)
# # 设置背景
# self.plotWidget.setBackground('w')
# # 增加图例
# # (1,2)中的2代表有2条线,系统会自动创建两种不同的颜色,1就代表这是第一条线
# self.plotWidget.addLegend()
# self.plotWidget.plot(x, y, pen=(1, 2), name='训练集')
# self.plotWidget.plot(x, b, pen=(2, 2), name='测试集')
# # 设置X轴,Y轴
# self.plotWidget.setLabel('left', 'LOSS')
# self.plotWidget.setLabel('bottom', 'epoch')
# # 设置网格
# self.plotWidget.showGrid(x=True, y=True)
# 设置背景
self.plotWidget.setBackground('w')
# 增加图例
# (1,2)中的2代表有2条线,系统会自动创建两种不同的颜色,1就代表这是第一条线
self.plotWidget.addLegend()
curve1 = self.plotWidget.plot(pen=(1, 2), name='训练集')
curve2 = self.plotWidget.plot(pen=(2, 2), name='测试集')
# 设置X轴,Y轴
self.plotWidget.setLabel('left', 'LOSS')
self.plotWidget.setLabel('bottom', 'epoch')
# 设置网格
self.plotWidget.showGrid(x=True, y=True)
curve1.setData(x, y)
curve2.setData(x, b)
if __name__ == "__main__":
import sys
app = QApplication(sys.argv)
ui = TestUI()
ui.show()
sys.exit(app.exec_())