matplotlib嵌入到pyqt中

在pyqt5中使用matplotlib

前言

雖然,qt中也提供了繪圖函數,但對於初學者並非很容易掌握,衆所周知,matplot提供了簡單,易用,強大的繪圖函數,結合mumpy基本能夠達到matlb中的繪圖體驗,而且比matlab更加具備擴展性,也更自由。經過matplotlib提供的官方例程的修改,就能夠很容易的繪製你想要的圖形,真的很強大。(我也是名初學者)python

matplotlib,pyqt5官方例程

# 取自matplotlib 官方文檔案例

from __future__ import unicode_literals
import sys
import os
import random
import matplotlib
# Make sure that we are using QT5
matplotlib.use('Qt5Agg')

from PyQt5 import QtCore, QtWidgets
from numpy import arange, sin, pi, linspace
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.figure import Figure

progname = os.path.basename(sys.argv[0])
progversion = "0.1"


class MyMplCanvas(FigureCanvas):
    # 這既是一個wiget類也是一個FigureCanva

    def __init__(self, parent=None, width=5, height=4, dpi=100):
        self.fig = Figure(figsize=(width, height), dpi=dpi)
        self.axes = self.fig.add_subplot(111)

        self.compute_initial_figure()

        FigureCanvas.__init__(self, self.fig)
        self.setParent(parent)

        FigureCanvas.setSizePolicy(self,
                                   QtWidgets.QSizePolicy.Expanding,
                                   QtWidgets.QSizePolicy.Expanding)
        FigureCanvas.updateGeometry(self)

    def compute_initial_figure(self):
        pass


class MyStaticMplCanvas(MyMplCanvas):
    """Simple canvas with a sine plot."""

    def __init__(self, *args, **kwargs):
        MyMplCanvas.__init__(self, *args, **kwargs)
        

    def compute_initial_figure(self):
        print("hello world")
        x = linspace(0, 2 * pi, 500000)
        y = sin(x)
        self.axes.cla()
        self.axes.plot(x, y)
        self.draw()


class MyDynamicMplCanvas(MyMplCanvas):
    """A canvas that updates itself every second with a new plot."""

    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.plot([0, 1, 2, 3], [1, 2, 0, 4], 'r')

    def update_figure(self):
        # Build a list of 4 random integers between 0 and 10 (both inclusive)
        l = [random.randint(0, 10) for i in range(4)]
        self.axes.cla()
        self.axes.plot([0, 1, 2, 3], l, 'r')
        self.draw()


class ApplicationWindow(QtWidgets.QMainWindow):
    def __init__(self):
        QtWidgets.QMainWindow.__init__(self)
        self.setAttribute(QtCore.Qt.WA_DeleteOnClose)
        self.setWindowTitle("application main window")

        self.file_menu = QtWidgets.QMenu('&File', self)
        self.file_menu.addAction('&Quit', self.fileQuit,
                                 QtCore.Qt.CTRL + QtCore.Qt.Key_Q)
        self.menuBar().addMenu(self.file_menu)

        self.help_menu = QtWidgets.QMenu('&Help', self)
        self.menuBar().addSeparator()
        self.menuBar().addMenu(self.help_menu)

        self.help_menu.addAction('&About', self.about)

        self.main_widget = QtWidgets.QWidget(self)

        l = QtWidgets.QVBoxLayout(self.main_widget)
        sc = MyStaticMplCanvas(self.main_widget, width=5, height=4, dpi=100)
        dc = MyDynamicMplCanvas(self.main_widget, width=5, height=4, dpi=100)
        l.addWidget(sc)
        l.addWidget(dc)

        self.main_widget.setFocus()
        self.setCentralWidget(self.main_widget)

        self.statusBar().showMessage("All hail matplotlib!", 2000)

    def fileQuit(self):
        self.close()

    def closeEvent(self, ce):
        self.fileQuit()

    def about(self):
        QtWidgets.QMessageBox.about(self, "About",
                                    """embedding_in_qt5.py example
Copyright 2005 Florent Rougon, 2006 Darren Dale, 2015 Jens H Nielsen

This program is a simple example of a Qt5 application embedding matplotlib
canvases.

It may be used and modified with no restriction; raw copies as well as
modified versions may be distributed without limitation.

This is modified from the embedding in qt4 example to show the difference
between qt4 and qt5""")

代碼解析

經過matplotlib.use('Qt5Agg'),這行代碼聲明matplotlib將要嵌入到pyqt5中,一樣經過這句,也能夠聲明將matplotlib嵌入到其餘的,gui界面中去,而後經過繼承FigureCanvas類來得到一個即便widget的類也是FigureCanva類的類,而後經過self.fig成員,生成一個繪圖類,並由其建立一個繪圖佈局,返回一個self.axes來管理繪圖佈局中的內容。座標軸,標題,標籤,圖形樣式(餅圖,柱狀圖,折線圖等)等等的設置都經過self.axes的成員函數來設置完成。剛開始的使用仍是比較雲裏霧裏的,如今就差很少了。我對官方例程作了些修改,具體的代碼,能夠到個人GitHub倉儲上查看Qt-learn-pyqt5-matplotlib裏面也有一些其餘的例子,應該還會不按期的更新,有興趣也能夠看看。下面只須要對這幾個類進行實例話,開啓qt的事件循環就能夠看到界面了,具體的能夠看個人github代碼,這裏就很少說了。git

寫在最後

由於自身能力有限,也不是科班出身,都是自學的,目前仍是一名學生,因此有未盡之處還請指正,不喜勿噴。謝謝。github

相關文章
相關標籤/搜索