pyqt5-FigureCanvas

 Figure类似于画板,而Canvas类似于画纸

FigureCanvasXAgg就是一个渲染器,渲染器的工作就是drawing,执行绘图的这个动作。渲染器是使物体显示在屏幕上。

FigureCanvasQTAgg详情如下:

matplotlib中FigureCanvasXAgg的理解_小__Q的博客-CSDN博客

第一步,通过matplotlib.backends.backend_qt5agg类来连接PyQt5,这里涉及到后端的概念

from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
import matplotlib
matplotlib.use("Qt5Agg")
from matplotlib.figure import Figure
from matplotlib import pyplot
 
pyplot.rcParams['font.sans-serif'] = ['SimHei']
pyplot.rcParams['axes.unicode_minus'] = False

第二步:具体图形代码实现,关键在于每次绘图后需要self.fig.canvas.draw() 刷新画布。 

class Figure_Canvas(FigureCanvas):    
    def __init__(self, parent=None, width=6, height=4, dpi=100):
        fig = Figure(figsize=(width, height), dpi=100)
        FigureCanvas.__init__(self, fig) # 初始化父类
        self.setParent(parent)
        self.axes = fig.add_subplot(111)
    #这里就是绘制图像、示例
    def month(self):
        x,y = month()  #获取x和y
        self.axes.plot(x, y, label='month', marker='o', color='b')       #使用axes进行绘图
        self.axes.set_title('WLG2')
        self.axes.plot(x, y)
    def year(self):
        x,y = year()
        self.axes.plot(x, y, label='year', marker='o',color='b')
        self.axes.set_title('WLG2')
        self.axes.plot(x, y)

    def demo(self):
        x, y = month()
        self.axes.plot(x, y, label='month', marker='o', color='b')
        self.axes.set_title('FUYANG-month')
        self.axes.plot(x, y)

    def demo2(self):
        x, y = demo2()
        self.axes.plot(x, y,label='23-years-average', marker='o', color='blue')
        self.axes.set_title('FUYANG-year')
        self.axes.plot(x, y)

 第三步,GUI上通过QGraphicsView控件呈现matplotlib画出来的图形

        #获取graphicsView的长和宽单位为像素,Figure的单位是inches,在DPI为100时,除以100转换为inches
        width = self.graphicsView.width()
        height = self.graphicsView.height()
 
        self.myImage = Figure_Bar(width/100, height/100)
        self.myGraphyScene = QGraphicsScene()    #场景
        #取消水平和垂直滚动条
        self.graphicsView.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff) 
        self.graphicsView.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
        self.myQGraphicsProxyWidget = self.myGraphyScene.addWidget(self.myImage)
        self.graphicsView.setScene(self.myGraphyScene)
 
        #每次显示不同图像,只需调用ShowImage方法
        self.myImage.ShowImage(self.axisXcontent, self.axisYcontent)

如通过按钮显示第二歩中的year(),将按钮连接即可。

详情见Matplotlib植入PyQt5 + QT5的UI呈现_第三步,创建一个qgraphicsscene,因为加载的图形(figurecanvas)不能直接放到_CtrlZ1的博客-CSDN博客

    def wlg2_year(self):
        wlg2_year = Figure_Canvas()#实例化类
        wlg2_year.year()#调用类方法,作图
        graphicscene = QtWidgets.QGraphicsScene()#创建一个QGraphicsScene,因为加载的图形(FigureCanvas)不能直接放到graphicview控件中,必须先放到graphicScene,然后再把graphicscene放到graphicview中
        graphicscene.addWidget(wlg2_year)#把图形放到QGraphicsScene中,注意:图形是作为一个QWidget放到QGraphicsScene中的
        self.graphicsView.setScene(graphicscene)#把QGraphicsScene放入QGraphicsView
        self.graphicsView.show()#调用show方法呈现图形

主函数如下:

if __name__ == '__main__':
    # 添加如下代码
    QtCore.QCoreApplication.setAttribute(QtCore.Qt.AA_EnableHighDpiScaling)
    app = QApplication(sys.argv)
    mainWindow = QMainWindow()
    ui = task.Ui_task()   #task
    ui.setupUi(mainWindow)
    mainWindow.show()
    sys.exit(app.exec_())

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值