PyQt5結(jié)合matplotlib繪圖的實(shí)現(xiàn)示例
參考網(wǎng)上的例子,實(shí)現(xiàn)了簡(jiǎn)單的matplotlib pyqt5繪圖
相關(guān)知識(shí)點(diǎn):
(1)pyqt5中添加控件要在布局中添加
(2)布局可以使用replaceWidget替換控件
(3)信號(hào)與槽機(jī)制
timer = QtCore.QTimer(self) timer.timeout.connect(self.update_figure) self.btnPlot.clicked.connect(self.plotButton_callback)
實(shí)現(xiàn)的效果
import sys from PyQt5 import QtCore, QtGui, uic from PyQt5.QtWidgets import QMainWindow, QAction, qApp, QApplication, QMessageBox, QVBoxLayout, QSizePolicy, QWidget from PyQt5.QtGui import QIcon from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas from matplotlib.figure import Figure import matplotlib.pyplot as plt import numpy as np qtCreatorFile = "matplotlib_ui.ui" # 使用uic加載 Ui_MainWindow, QtBaseClass = uic.loadUiType(qtCreatorFile) class MyMplCanvas(FigureCanvas): """這是一個(gè)窗口部件,即QWidget(當(dāng)然也是FigureCanvasAgg)""" def __init__(self, parent=None, width=5, height=4, dpi=100): fig = Figure(figsize=(width, height), dpi=dpi) self.axes = fig.add_subplot(111) # 每次plot()調(diào)用的時(shí)候,我們希望原來(lái)的坐標(biāo)軸被清除(所以False) self.axes.hold(False) self.axes.grid('on') self.compute_initial_figure() # FigureCanvas.__init__(self, fig) self.setParent(parent) FigureCanvas.setSizePolicy(self, QSizePolicy.Expanding, QSizePolicy.Expanding) FigureCanvas.updateGeometry(self) def compute_initial_figure(self): pass class MyStaticMplCanvas(MyMplCanvas): """靜態(tài)畫(huà)布:一條正弦線""" def compute_initial_figure(self): t = np.arange(0.0, 3.0, 0.01) s = np.sin(2 * np.pi * t) self.axes.grid('on') self.axes.plot(t, s) class MyDynamicMplCanvas(MyMplCanvas): """動(dòng)態(tài)畫(huà)布:每秒自動(dòng)更新,更換一條折線。""" def __init__(self, *args, **kwargs): MyMplCanvas.__init__(self, *args, **kwargs) timer = QtCore.QTimer(self) timer.timeout.connect(self.update_figure) timer.start(1000) def compute_initial_figure(self): self.axes.grid('on') self.axes.plot([0, 1, 2, 3], [1, 2, 0, 4], 'r') def update_figure(self): # 構(gòu)建4個(gè)隨機(jī)整數(shù),位于閉區(qū)間[0, 10] l = [np.random.randint(0, 10) for i in range(4)] self.axes.grid('on') self.axes.plot([0, 1, 2, 3], l, 'r') self.draw() class MyApp(QMainWindow, Ui_MainWindow): def __init__(self): QMainWindow.__init__(self) Ui_MainWindow.__init__(self) super().__init__() self.initUI() self.initBtn() self.initFrame() def initFrame(self): self.main_widget = self.frame self.layout = QVBoxLayout(self.main_widget) self.f = MyMplCanvas(self.main_widget) self.layout.addWidget(self.f) def initUI(self): self.setupUi(self) self.setWindowTitle("PyQt5結(jié)合Matplotlib繪圖") self.setWindowIcon(QIcon("rocket.ico")) # 設(shè)置圖標(biāo),linux下只有任務(wù)欄會(huì)顯示圖標(biāo) self.show() def initBtn(self): self.btnPlot.clicked.connect(self.plotButton_callback) self.btnPlot.setToolTip("Button") def plotButton_callback(self): self.drawFrame() def drawFrame(self): sc = MyStaticMplCanvas(self.main_widget, width=5, height=4, dpi=100) dc = MyDynamicMplCanvas(self.f, width=5, height=4, dpi=100) self.layout.addWidget(sc) self.layout.replaceWidget(self.f,dc) # 替換控件 if __name__ == '__main__': app = QApplication(sys.argv) ex = MyApp() sys.exit(app.exec_())
參考鏈接:https://www.cnblogs.com/hhh5460/p/4322652.html
到此這篇關(guān)于PyQt5結(jié)合matplotlib繪圖的實(shí)現(xiàn)示例的文章就介紹到這了,更多相關(guān)PyQt5結(jié)合matplotlib內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- PyQt5 matplotlib畫(huà)圖不刷新的解決方案
- 詳解pyqt5的UI中嵌入matplotlib圖形并實(shí)時(shí)刷新(挖坑和填坑)
- 利用PyQt5+Matplotlib 繪制靜態(tài)/動(dòng)態(tài)圖的實(shí)現(xiàn)代碼
- pyqt5與matplotlib的完美結(jié)合實(shí)例
- python GUI庫(kù)圖形界面開(kāi)發(fā)之PyQt5滾動(dòng)條控件QScrollBar詳細(xì)使用方法與實(shí)例
- 在PYQT5中QscrollArea(滾動(dòng)條)的使用方法
- PyQt5實(shí)現(xiàn)將Matplotlib圖像嵌入到Scoll Area中顯示滾動(dòng)條效果
相關(guān)文章
python命令行執(zhí)行腳本找不到模塊ModuleNotFoundError問(wèn)題
這篇文章主要介紹了python命令行執(zhí)行腳本找不到模塊ModuleNotFoundError問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-06-06python實(shí)現(xiàn)excel讀寫(xiě)數(shù)據(jù)
這篇文章主要為大家詳細(xì)介紹了python操作EXCEL讀數(shù)據(jù)、寫(xiě)數(shù)據(jù)的實(shí)例源碼,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-04-04python調(diào)用動(dòng)態(tài)鏈接庫(kù)的基本過(guò)程詳解
這篇文章主要介紹了python調(diào)用動(dòng)態(tài)鏈接庫(kù)的基本過(guò)程詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-06-06Python 中 -m 的典型用法、原理解析與發(fā)展演變
這篇文章主要介紹了Python 中 -m 的典型用法、原理解析與發(fā)展演變,需要的朋友可以參考下2019-11-11python實(shí)現(xiàn)簡(jiǎn)易計(jì)算器功能
這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)簡(jiǎn)易計(jì)算器功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-02-02如何使用python的opencv實(shí)現(xiàn)人臉識(shí)別
這篇文章主要介紹了使用python的opencv實(shí)現(xiàn)人臉識(shí)別功能,本項(xiàng)目主要使用python語(yǔ)言,主要的模塊庫(kù)有os,opencv-python,opencv-contrib-python,需要的朋友可以參考下2023-12-12