matplotlib图形 嵌入 PyQt5

本篇介绍如何 将由matplotlib绘就的图形 嵌入到 PyQt5界面中。

只需从matplotlib.backends.backend_qt5agg.FigureCanvasQTAgg类继承一个画布控件,然后就可以当成是 PyQt5 普通控件那样添加到图形界面即可。详情可参考如下例子:

import sys
import numpy as np
import matplotlib
from PyQt5 import QtCore, QtWidgets
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg
from matplotlib.figure import Figure
#画布控件继承自 matplotlib.backends.backend_qt5agg.FigureCanvasQTAgg 类
class Canvas(FigureCanvasQTAgg):
    def __init__(self, parent=None, width=5, height=4, dpi=100):
        fig = Figure(figsize=(width, height), dpi=dpi) #创建画布,设置宽高,每英寸像素点数
        self.axes = fig.add_subplot(111)#
        FigureCanvasQTAgg.__init__(self, fig)#调用基类的初始化函数
        self.setParent(parent)
        self.update_figure()
        FigureCanvasQTAgg.updateGeometry(self)
    def update_figure(self, x=np.random.randn(10) ,y =np.random.randn(10)):
        self.axes.cla()#清除已绘的图形
        self.axes.scatter(x,y,  marker ='*',c=np.random.rand(len(x)), cmap = "jet")
        self.draw()#重新绘制
    
class Widget(QtWidgets.QWidget):
    def __init__(self):
        super().__init__()
        #创建 matplotlib画布控件
        self.myfigure = Canvas(self,dpi=200)
        layout = QtWidgets.QVBoxLayout()
        layout.addWidget(self.myfigure)#添加到布局
        self.button = QtWidgets.QPushButton("update")
        self.button.setFixedWidth(100)
        self.button.clicked.connect(lambda : self.replot(
            x = np.random.randn(10),y =np.random.randn(10)+1))
        layout.addWidget(self.button)#添加到布局
        self.setLayout(layout)
        self.resize(600,400)
        self.setWindowTitle("matplotlib 图形 嵌入 PyQt5")
       
    def replot(self, x, y):
        self.myfigure.update_figure(x,y)

qApp = QtWidgets.QApplication(sys.argv)
ui = Widget()
ui.show()
sys.exit(qApp.exec_())

下面这个例子来自官网,稍微复杂一点点,我仅添加了一点中文注释。

from __future__ import unicode_literals
import sys
import os
import random
import matplotlib
# Make sure that we are using QT5
matplotlib.use('Qt5Agg')
# Uncomment this line before running, it breaks sphinx-gallery builds
from PyQt5 import QtCore, QtWidgets
from numpy import arange, sin, pi
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.figure import Figure
progname = os.path.basename(sys.argv[0]) #py程序名
print(progname) 
progversion = "0.1"
#画布控件继承自 matplotlib.backends.backend_qt5agg.FigureCanvasQTAgg 类
class MyMplCanvas(FigureCanvas): 
    """Ultimately, this is a QWidget (as well as a FigureCanvasAgg, etc.)."""
    def __init__(self, parent=None, width=5, height=4, dpi=100):
        fig = Figure(figsize=(width, height), dpi=dpi)
        self.axes = fig.add_subplot(111)
        self.compute_initial_figure()
        FigureCanvas.__init__(self, fig)#调用基类的初始化函数
        self.setParent(parent)
        
        #尺寸缩放策略
        FigureCanvas.setSizePolicy(self,
                                   QtWidgets.QSizePolicy.Expanding,
                                   QtWidgets.QSizePolicy.Expanding)
        FigureCanvas.updateGeometry(self)
    def compute_initial_figure(self):
        pass #再次被继承时被同名方法覆盖,所有这里可为pass
#再继承第一个自定义画布控件类
class MyStaticMplCanvas(MyMplCanvas):#静态画布类
    """Simple canvas with a sine plot."""
    def compute_initial_figure(self):
        t = arange(0.0, 3.0, 0.01)
        s = sin(2*pi*t)
        self.axes.plot(t, s)
#再继承第一个自定义画布控件类
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)#每1000秒发射timeout信号,更新图形
    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)
        
        #matplotlib画布控件和普通PyQt的用法一样
        #创建 matplotlib画布控件
        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"""
                                )

qApp = QtWidgets.QApplication(sys.argv)
aw = ApplicationWindow()
aw.setWindowTitle("%s" % progname)
aw.show()
sys.exit(qApp.exec_())

原文发布于微信公众号 - Python编程 pyqt matplotlib(wsplovePython)

原文发表时间:2019-05-18

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

扫码关注云+社区

领取腾讯云代金券