matplotlib的图形处理非常强大。今天花了很长时间才将matplotlib图形嵌入到pyqt5中。
在这里记录一下,便于以后查寻。有些可能还理解不到位。
开始要导入几个模块:
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
FigureCanvasQTAgg是一个渲染器,它的工作就是不停的drawing,从而将物体显示在屏幕上。在这里可以理解
为将matplotlib中的figure对象变为一个Qt widgets.
给界面中的groupBox添加一个布局管理器,代码为:
layout=QVBoxLayout(self.groupBox)
把渲染器添加到该布局中代码为:
layout.addWidget(self.static_canvas)
从渲染器的画布figure中,然后就可以进行子布局,渲染器为:self.static_canvas ,画布为:self.static_canvas.figure
然后单个创建的方法为(eg:创建2行列):ax1=self.self.static_canvas.figure.add_subplot(211),
ax2=self.self.static_canvas.figure.add_subplot(212)
第二种方法:ax1,ax2= self.static_canvas.figure.subplots(2,1)
4.如果要向会矩形,圆形也可以。先导入from matplotlib.pyplot import Rectangle
# ax1=self.static_canvas.figure.add_subplot(111)
# # rect=Rectangle((0.3,0.75),0.6,0.7,color='g')
# rect=Rectangle((3,3),6,8,color='g')
# ax1.add_patch(rect)
完整代码:
from PyQt5.Qt import *
from PyQt5.uic import loadUi
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
import numpy as np
from matplotlib.figure import Figure
from matplotlib.pyplot import Rectangle
class Window(QWidget):
def __init__(self):
super().__init__()
# loadUi('plot_gui.ui',self)
# self.setWindowTitle("的学习")
layout=QVBoxLayout()
self.groupBox=QGroupBox()
self.groupBox_2=QGroupBox()
layout.addWidget(self.groupBox)
layout.addWidget(self.groupBox_2)
self.setLayout(layout)
self.setup_ui()
def setup_ui(self):
self.static_canvas = FigureCanvas(Figure(figsize=(5,3)))
layout=QVBoxLayout(self.groupBox)
layout.addWidget(self.static_canvas)
x=np.linspace(0,2*np.pi)
y=np.cos(x)
ax1,ax2= self.static_canvas.figure.subplots(2,1)
ax1.plot(x,y)
# ax1=self.static_canvas.figure.add_subplot(111)
# ax1.plot(x,y)
# ax1=self.static_canvas.figure.add_subplot(111)
# # rect=Rectangle((0.3,0.75),0.6,0.7,color='g')
# rect=Rectangle((3,3),6,8,color='g')
# ax1.add_patch(rect)
if __name__ == '__main__':
import sys
app=QApplication(sys.argv)
win=Window()
win.show()
from PyQt5.Qt import *
from PyQt5.uic import loadUi
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.backends.backend_qt5agg import NavigationToolbar2QT as NavigationToolbar
import numpy as np
from matplotlib.figure import Figure
from matplotlib.pyplot import Rectangle
class Window(QWidget):
def __init__(self):
super().__init__()
# loadUi('plot_gui.ui',self)
# self.setWindowTitle("的学习")
self.resize(900,700)
layout = QVBoxLayout()
self.groupBox = QGroupBox()
self.groupBox_2 = QGroupBox()
layout.addWidget(self.groupBox)
layout.addWidget(self.groupBox_2)
self.setLayout(layout)
self.setup_ui()
def setup_ui(self):
'''创建布局器,然后布局器里添加控件 比如画布,工具'''
self.static_canvas = FigureCanvas(Figure(figsize=(5, 3)))#创建一个画布
layout = QVBoxLayout(self.groupBox)
layout.addWidget(self.static_canvas)
x = np.linspace(0, 2 * np.pi)
y = np.cos(x)
self.ax1, self.ax2 = self.static_canvas.figure.subplots(2, 1)
self.ax1.plot(x, y)
# ax1=self.static_canvas.figure.add_subplot(111)
# ax1.plot(x,y)
# ax1=self.static_canvas.figure.add_subplot(111)
# # rect=Rectangle((0.3,0.75),0.6,0.7,color='g')
# rect=Rectangle((3,3),6,8,color='g')
# ax1.add_patch(rect)
self.timer = QTimer()
self.timer.timeout.connect(self.refresh_plot)
self.timer.start(100)
self.i = 0
self.t = []
self.s = []
self.canvas_2 = FigureCanvas(Figure()) #创建第二个画布
tool_bar = NavigationToolbar(self.canvas_2,self.groupBox_2)
layout2 = QVBoxLayout(self.groupBox_2) #创建布局器
layout2.addWidget(self.canvas_2) #添加画布
layout2.addWidget(tool_bar) #添加工具
t=np.linspace(0,10,501)
self.ax3=self.canvas_2.figure.subplots()
self.ax3.plot(t,np.sin(t)*np.cos(2*t))
def refresh_plot(self):
self.i += 0.01
self.t.append(self.i)
self.s.append(np.sin(2*np.pi*self.i))
self.ax2.cla()
self.ax2.plot(self.t,self.s)
self.static_canvas.draw()
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
win = Window()
win.show()
sys.exit(app.exec_())