pyqt5与matplotlib的完美结合实例
具体用到了matplotlib.backends.backend_qt5agg.FigureCanvasQTAgg
直接上代码(这里给出的只是一个简单的框架,告诉你怎么去写):
#-*-coding:utf-8-*- ''' TODO:LQD ''' importsys importnumpyasnp importmatplotlib.pyplotasplt frommatplotlib.backends.backend_qt5aggimportFigureCanvasQTAggasFC fromPyQt5.QtWidgetsimportQApplication,QPushButton,QMainWindow,QVBoxLayout,QWidget classQtDraw(QMainWindow): flag_btn_start=True def__init__(self): super(QtDraw,self).__init__() self.init_ui() definit_ui(self): self.resize(800,600) self.setWindowTitle('PyQt5Draw') #TODO:这里是结合的关键 self.fig=plt.Figure() self.canvas=FC(self.fig) self.btn_start=QPushButton(self) self.btn_start.setText('draw') self.btn_start.clicked.connect(self.slot_btn_start) widget=QWidget() layout=QVBoxLayout() layout.addWidget(self.canvas) layout.addWidget(self.btn_start) widget.setLayout(layout) self.setCentralWidget(widget) defslot_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:这里开始绘制 exceptExceptionase: print(e) defui_main(): app=QApplication(sys.argv) w=QtDraw() w.show() sys.exit(app.exec_()) if__name__=='__main__': ui_main()
以上这篇pyqt5与matplotlib的完美结合实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持毛票票。