pymatplotlib-1 绘制2D矩形图(pyqt)

pymatplotlib-1 绘制2D矩形图

import matplotlib.pyplot as plt

def showRectangle(data):
    fig = plt.figure("绘制2D矩形图")
    ax = fig.add_subplot(111, aspect='equal')
    Rect = plt.Rectangle(
    					(data[0],data[1]),   #(x,y)表示矩形左下角的坐标
    					data[2],  # width,矩形宽度
    					data[3],  # height,矩形高度
    					color='blue', #颜色
            			alpha=1)  # 透明度
    ax.add_patch(Rect)
    # plt.axis('off')  # 关闭刻度
    plt.xlim(0, 5)  # x轴坐标范围
    plt.ylim(0, 5)  # y轴坐标范围
    plt.show()       # 显示
    # fig.savefig('rect1.png', dpi=90, bbox_inches='tight')
if __name__ == "__main__":
 	data = (1,1,3,2)
 	showRectangle(data)

效果图:
在这里插入图片描述

在pyqt中使用pymatplotlib展示图形:
1、在Qt Designer 中设计界面UI
在这里插入图片描述
Ui_rectange.py代码:

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'e:\Mypython\3D\rectange.ui'
#
# Created by: PyQt5 UI code generator 5.15.0
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again.  Do not edit this file unless you know what you are doing.


from PyQt5 import QtCore, QtGui, QtWidgets


class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(588, 548)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.verticalLayout_4 = QtWidgets.QVBoxLayout(self.centralwidget)
        self.verticalLayout_4.setObjectName("verticalLayout_4")
        self.verticalLayout_3 = QtWidgets.QVBoxLayout()
        self.verticalLayout_3.setObjectName("verticalLayout_3")
        self.groupBox = QtWidgets.QGroupBox(self.centralwidget)
        self.groupBox.setMinimumSize(QtCore.QSize(300, 300))
        self.groupBox.setObjectName("groupBox")
        self.verticalLayout_3.addWidget(self.groupBox)
        self.horizontalLayout_6 = QtWidgets.QHBoxLayout()
        self.horizontalLayout_6.setObjectName("horizontalLayout_6")
        spacerItem = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
        self.horizontalLayout_6.addItem(spacerItem)
        self.checkBox = QtWidgets.QCheckBox(self.centralwidget)
        self.checkBox.setChecked(True)
        self.checkBox.setObjectName("checkBox")
        self.horizontalLayout_6.addWidget(self.checkBox)
        self.verticalLayout_3.addLayout(self.horizontalLayout_6)
        self.horizontalLayout_5 = QtWidgets.QHBoxLayout()
        self.horizontalLayout_5.setObjectName("horizontalLayout_5")
        self.verticalLayout = QtWidgets.QVBoxLayout()
        self.verticalLayout.setObjectName("verticalLayout")
        self.horizontalLayout = QtWidgets.QHBoxLayout()
        self.horizontalLayout.setObjectName("horizontalLayout")
        self.label = QtWidgets.QLabel(self.centralwidget)
        self.label.setObjectName("label")
        self.horizontalLayout.addWidget(self.label)
        self.doubleSpinBox = QtWidgets.QDoubleSpinBox(self.centralwidget)
        self.doubleSpinBox.setDecimals(0)
        self.doubleSpinBox.setObjectName("doubleSpinBox")
        self.horizontalLayout.addWidget(self.doubleSpinBox)
        self.doubleSpinBox_2 = QtWidgets.QDoubleSpinBox(self.centralwidget)
        self.doubleSpinBox_2.setDecimals(0)
        self.doubleSpinBox_2.setProperty("value", 10.0)
        self.doubleSpinBox_2.setObjectName("doubleSpinBox_2")
        self.horizontalLayout.addWidget(self.doubleSpinBox_2)
        self.verticalLayout.addLayout(self.horizontalLayout)
        self.horizontalLayout_2 = QtWidgets.QHBoxLayout()
        self.horizontalLayout_2.setObjectName("horizontalLayout_2")
        self.label_2 = QtWidgets.QLabel(self.centralwidget)
        self.label_2.setObjectName("label_2")
        self.horizontalLayout_2.addWidget(self.label_2)
        self.doubleSpinBox_4 = QtWidgets.QDoubleSpinBox(self.centralwidget)
        self.doubleSpinBox_4.setDecimals(0)
        self.doubleSpinBox_4.setObjectName("doubleSpinBox_4")
        self.horizontalLayout_2.addWidget(self.doubleSpinBox_4)
        self.doubleSpinBox_3 = QtWidgets.QDoubleSpinBox(self.centralwidget)
        self.doubleSpinBox_3.setDecimals(0)
        self.doubleSpinBox_3.setProperty("value", 10.0)
        self.doubleSpinBox_3.setObjectName("doubleSpinBox_3")
        self.horizontalLayout_2.addWidget(self.doubleSpinBox_3)
        self.verticalLayout.addLayout(self.horizontalLayout_2)
        self.horizontalLayout_5.addLayout(self.verticalLayout)
        self.verticalLayout_2 = QtWidgets.QVBoxLayout()
        self.verticalLayout_2.setObjectName("verticalLayout_2")
        self.horizontalLayout_3 = QtWidgets.QHBoxLayout()
        self.horizontalLayout_3.setObjectName("horizontalLayout_3")
        self.label_3 = QtWidgets.QLabel(self.centralwidget)
        self.label_3.setObjectName("label_3")
        self.horizontalLayout_3.addWidget(self.label_3)
        self.doubleSpinBox_6 = QtWidgets.QDoubleSpinBox(self.centralwidget)
        self.doubleSpinBox_6.setDecimals(0)
        self.doubleSpinBox_6.setProperty("value", 1.0)
        self.doubleSpinBox_6.setObjectName("doubleSpinBox_6")
        self.horizontalLayout_3.addWidget(self.doubleSpinBox_6)
        self.doubleSpinBox_5 = QtWidgets.QDoubleSpinBox(self.centralwidget)
        self.doubleSpinBox_5.setDecimals(0)
        self.doubleSpinBox_5.setProperty("value", 1.0)
        self.doubleSpinBox_5.setObjectName("doubleSpinBox_5")
        self.horizontalLayout_3.addWidget(self.doubleSpinBox_5)
        self.verticalLayout_2.addLayout(self.horizontalLayout_3)
        self.horizontalLayout_4 = QtWidgets.QHBoxLayout()
        self.horizontalLayout_4.setObjectName("horizontalLayout_4")
        self.label_4 = QtWidgets.QLabel(self.centralwidget)
        self.label_4.setObjectName("label_4")
        self.horizontalLayout_4.addWidget(self.label_4)
        self.doubleSpinBox_7 = QtWidgets.QDoubleSpinBox(self.centralwidget)
        self.doubleSpinBox_7.setDecimals(0)
        self.doubleSpinBox_7.setProperty("value", 3.0)
        self.doubleSpinBox_7.setObjectName("doubleSpinBox_7")
        self.horizontalLayout_4.addWidget(self.doubleSpinBox_7)
        self.doubleSpinBox_8 = QtWidgets.QDoubleSpinBox(self.centralwidget)
        self.doubleSpinBox_8.setDecimals(0)
        self.doubleSpinBox_8.setProperty("value", 2.0)
        self.doubleSpinBox_8.setObjectName("doubleSpinBox_8")
        self.horizontalLayout_4.addWidget(self.doubleSpinBox_8)
        self.verticalLayout_2.addLayout(self.horizontalLayout_4)
        self.horizontalLayout_5.addLayout(self.verticalLayout_2)
        self.verticalLayout_3.addLayout(self.horizontalLayout_5)
        self.verticalLayout_4.addLayout(self.verticalLayout_3)
        MainWindow.setCentralWidget(self.centralwidget)

        self.retranslateUi(MainWindow)
        self.checkBox.stateChanged['int'].connect(MainWindow.showRectangle)
        self.doubleSpinBox.valueChanged['QString'].connect(MainWindow.showRectangle)
        self.doubleSpinBox_2.valueChanged['QString'].connect(MainWindow.showRectangle)
        self.doubleSpinBox_4.valueChanged['QString'].connect(MainWindow.showRectangle)
        self.doubleSpinBox_3.valueChanged['QString'].connect(MainWindow.showRectangle)
        self.doubleSpinBox_6.valueChanged['QString'].connect(MainWindow.showRectangle)
        self.doubleSpinBox_5.valueChanged['double'].connect(MainWindow.showRectangle)
        self.doubleSpinBox_8.valueChanged['QString'].connect(MainWindow.showRectangle)
        self.doubleSpinBox_7.valueChanged['QString'].connect(MainWindow.showRectangle)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        self.groupBox.setTitle(_translate("MainWindow", "GroupBox"))
        self.checkBox.setText(_translate("MainWindow", "坐标轴"))
        self.label.setText(_translate("MainWindow", "X坐标轴范围:"))
        self.label_2.setText(_translate("MainWindow", "y坐标轴范围:"))
        self.label_3.setText(_translate("MainWindow", "矩形坐标:"))
        self.label_4.setText(_translate("MainWindow", "矩形宽高:"))

2、主程序
关键是要在groupBox中继承gridlayout,再将 FigureCanvas画布 通过addWidget加入

   self.gridlayout = QGridLayout(self.groupBox)    # 继承容器groupBox
   self.canvas = FigureCanvas(self.figure)         #增加画布
   self.gridlayout.addWidget(self.canvas)          #将画布加入到groupBox中

主程序完整代码:

import sys
from Ui_rectange import Ui_MainWindow
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from PyQt5.QtWidgets import QApplication,QMainWindow,QGridLayout
from matplotlib import pyplot as plt

class mwindow(QMainWindow,Ui_MainWindow):
    def __init__(self):
        super().__init__()
        self.setupUi(self)
        self.initUI()
        self.showRectangle()

    def initUI(self):
        self.figure = plt.figure("绘制2D矩形图",facecolor="#cfcfcf", clear=True)  # 实例化一个fig
        self.ax = self.figure.add_subplot(111, aspect='equal')

        self.gridlayout = QGridLayout(self.groupBox)    # 继承容器groupBox
        self.canvas = FigureCanvas(self.figure)         #增加画布
        self.gridlayout.addWidget(self.canvas)          #将画布加入到groupBox中

    def showRectangle(self):
        self.ax.clear()
        Rect = plt.Rectangle(
                            (self.doubleSpinBox_6.value(),self.doubleSpinBox_5.value()),   #(x,y)表示矩形左下角的坐标
                            self.doubleSpinBox_7.value(),  # width,矩形宽度
                            self.doubleSpinBox_8.value(),  # height,矩形高度
                            color='blue', #颜色
                            alpha=1)  # 透明度
        self.ax.add_patch(Rect)
            
        plt.xlim(self.doubleSpinBox.value(), self.doubleSpinBox_2.value())  # x轴坐标范围
        plt.ylim(self.doubleSpinBox_4.value(), self.doubleSpinBox_3.value())  # y轴坐标范围

        if self.checkBox.isChecked():
            plt.axis('on')  # 关闭刻度
        else:
            plt.axis('off')  # 关闭刻度
        self.canvas.draw()      # 显示

if __name__ == '__main__':
    app = QApplication(sys.argv)
    w = mwindow()
    w.show()
    sys.exit(app.exec_())

效果如下:
在这里插入图片描述
如果不想使用groupBox,也可以直接在gridlayout或horizontalLayout、verticalLayout中放入 FigureCanvas画布,也能达到在PYQT中使用pymatplotlib绘图的同样目的。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值