pyqt5与matplotlib的完美结合实例
具体⽤到了matplotlib.backends.backend_qt5agg.FigureCanvasQTAgg
直接上代码(这⾥给出的只是⼀个简单的框架,告诉你怎么去写):
# -*- coding: utf-8 -*-
'''
TODO:LQD
'''
import sys
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FC
from PyQt5.QtWidgets import QApplication, QPushButton, QMainWindow, QVBoxLayout, QWidget
class QtDraw(QMainWindow):
flag_btn_start = True
def __init__(self):
matplotlib中subplotsuper(QtDraw, self).__init__()
self.init_ui()
def init_ui(self):
self.setWindowTitle('PyQt5 Draw')
# TODO:这⾥是结合的关键
self.fig = plt.Figure()
self.canvas = FC(self.fig)
self.btn_start = QPushButton(self)
self.btn_start.setText('draw')
self.btn_t(self.slot_btn_start)
widget = QWidget()
layout = QVBoxLayout()
layout.addWidget(self.canvas)
layout.addWidget(self.btn_start)
widget.setLayout(layout)
self.setCentralWidget(widget)
def slot_btn_start(self):
try:
ax = self.fig.add_subplot(111)
x = np.linspace(0, 100, 100)
y = np.random.random(100)
ax.cla() # TODO:删除原图,让画布上只有新的⼀次的图
ax.plot(x, y)
self.canvas.draw() # TODO:这⾥开始绘制
except Exception as e:
print(e)
def ui_main():
app = QApplication(sys.argv)
w = QtDraw()
w.show()
<_())
if __name__ == '__main__':
ui_main()
以上这篇pyqt5与matplotlib的完美结合实例就是⼩编分享给⼤家的全部内容了,希望能给⼤家⼀个参考,也希望⼤家多多⽀持。

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。