欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

PyQt5結(jié)合matplotlib繪圖的實(shí)現(xiàn)示例

 更新時(shí)間:2020年09月15日 14:27:11   作者:落葉_小唱  
這篇文章主要介紹了PyQt5結(jié)合matplotlib繪圖的實(shí)現(xiàn)示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧

參考網(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)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • python命令行執(zhí)行腳本找不到模塊ModuleNotFoundError問(wèn)題

    python命令行執(zhí)行腳本找不到模塊ModuleNotFoundError問(wèn)題

    這篇文章主要介紹了python命令行執(zhí)行腳本找不到模塊ModuleNotFoundError問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-06-06
  • python實(shí)現(xiàn)excel讀寫(xiě)數(shù)據(jù)

    python實(shí)現(xiàn)excel讀寫(xiě)數(shù)據(jù)

    這篇文章主要為大家詳細(xì)介紹了python操作EXCEL讀數(shù)據(jù)、寫(xiě)數(shù)據(jù)的實(shí)例源碼,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-04-04
  • python調(diào)用動(dòng)態(tài)鏈接庫(kù)的基本過(guò)程詳解

    python調(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-06
  • python使用matplotlib繪制折線圖教程

    python使用matplotlib繪制折線圖教程

    Matplotlib是一個(gè)Python工具箱,用于科學(xué)計(jì)算的數(shù)據(jù)可視化。借助它,Python可以繪制如Matlab和Octave多種多樣的數(shù)據(jù)圖形。下面這篇文章主要介紹了python使用matplotlib如何繪制折線圖的方法教程,需要的朋友可以參考借鑒。
    2017-02-02
  • Python 中 -m 的典型用法、原理解析與發(fā)展演變

    Python 中 -m 的典型用法、原理解析與發(fā)展演變

    這篇文章主要介紹了Python 中 -m 的典型用法、原理解析與發(fā)展演變,需要的朋友可以參考下
    2019-11-11
  • python下載文件的兩種方式

    python下載文件的兩種方式

    這篇文章主要介紹了python下載文件的兩種方式:接口方式和Nginx這兩種方式,并通過(guò)代碼示例講解的非常詳細(xì),對(duì)大家學(xué)習(xí)python下載文件有一定的幫助,需要的朋友可以參考下
    2024-08-08
  • 一篇不錯(cuò)的Python入門(mén)教程

    一篇不錯(cuò)的Python入門(mén)教程

    一篇不錯(cuò)的Python入門(mén)教程...
    2007-02-02
  • Python深入學(xué)習(xí)之裝飾器

    Python深入學(xué)習(xí)之裝飾器

    這篇文章主要介紹了Python深入學(xué)習(xí)之裝飾器,裝飾器(decorator)是一種高級(jí)Python語(yǔ)法,本文全面介紹了Python中的裝飾器,需要的朋友可以參考下
    2014-08-08
  • python實(shí)現(xiàn)簡(jiǎn)易計(jì)算器功能

    python實(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í)別

    這篇文章主要介紹了使用python的opencv實(shí)現(xiàn)人臉識(shí)別功能,本項(xiàng)目主要使用python語(yǔ)言,主要的模塊庫(kù)有os,opencv-python,opencv-contrib-python,需要的朋友可以參考下
    2023-12-12

最新評(píng)論