日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發布時間:2024/3/26 编程问答 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 pymatplotlib-1 绘制2D矩形图(pyqt) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

pymatplotlib-1 繪制2D矩形圖

import matplotlib.pyplot as pltdef 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, QtWidgetsclass 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.translateMainWindow.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) # 繼承容器groupBoxself.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 pltclass 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) # 實例化一個figself.ax = self.figure.add_subplot(111, aspect='equal')self.gridlayout = QGridLayout(self.groupBox) # 繼承容器groupBoxself.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繪圖的同樣目的。

總結

以上是生活随笔為你收集整理的pymatplotlib-1 绘制2D矩形图(pyqt)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。