生活随笔
收集整理的這篇文章主要介紹了
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]), data
[2], data
[3], color
='blue', alpha
=1) ax
.add_patch
(Rect
)plt
.xlim
(0, 5) plt
.ylim
(0, 5) plt
.show
()
if __name__
== "__main__":data
= (1,1,3,2)showRectangle
(data
)
效果圖:
在pyqt中使用pymatplotlib展示圖形:
1、在Qt Designer 中設計界面UI
Ui_rectange.py代碼:
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
.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
) self
.canvas
= FigureCanvas
(self
.figure
) self
.gridlayout
.addWidget
(self
.canvas
)
主程序完整代碼:
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) self
.ax
= self
.figure
.add_subplot
(111, aspect
='equal')self
.gridlayout
= QGridLayout
(self
.groupBox
) self
.canvas
= FigureCanvas
(self
.figure
) self
.gridlayout
.addWidget
(self
.canvas
) def showRectangle(self
):self
.ax
.clear
()Rect
= plt
.Rectangle
((self
.doubleSpinBox_6
.value
(),self
.doubleSpinBox_5
.value
()), self
.doubleSpinBox_7
.value
(), self
.doubleSpinBox_8
.value
(), color
='blue', alpha
=1) self
.ax
.add_patch
(Rect
)plt
.xlim
(self
.doubleSpinBox
.value
(), self
.doubleSpinBox_2
.value
()) plt
.ylim
(self
.doubleSpinBox_4
.value
(), self
.doubleSpinBox_3
.value
()) 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)的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。