为PyQt5嵌入式图传递matplotlib图/轴

时间:2018-11-13 14:08:59

标签: python matplotlib pyqt5

昨天我有一个问题,关于如何使用工具栏更新pyqt5 gui中的嵌入式matplotlib图,该工具现在可以正常工作。但是在我的应用程序中,与最小示例相比,它更加复杂。

当GUI在另一个文件中的单独类中时,我有一个类通过matplotlib进行分析并创建图。我做了一个修改过的最小示例:

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

import sys
import numpy as np

from PyQt5 import QtCore, QtWidgets, uic

import matplotlib
matplotlib.use('QT5Agg')

import matplotlib.pylab as plt

from matplotlib.backends.qt_compat import QtCore, QtWidgets, is_pyqt5
from matplotlib.backends.backend_qt5agg import FigureCanvas, NavigationToolbar2QT as NavigationToolbar
from matplotlib.figure import Figure

class Analysis():

    def __init__(self):

        self.fig, self.ax1 = plt.subplots()

        bins = np.arange(0.1, 2, 0.02)
        data = np.random.rand(3,2)        
        self.ax1.hist(data, bins, alpha=0.6, density=False, cumulative=False)      

    def getAxis(self):
        return self.ax1


class MyWindow(QtWidgets.QMainWindow):

    def __init__(self):

        super(MyWindow, self).__init__()
        uic.loadUi('test.ui', self) 

        # default data
        data = np.array([0.7,0.7,0.7,0.8,0.9,0.9,1.5,1.5,1.5,1.5])        
        self.fig, self.ax1 = plt.subplots()

        bins = np.arange(0.1, 2, 0.02)
        n1, bins1, patches1 = self.ax1.hist(data, bins, alpha=0.6, density=False, cumulative=False)

        # plot
        self.plotWidget = FigureCanvas(self.fig)
        self.lay = QtWidgets.QVBoxLayout(self.content_plot)  
        self.lay.setContentsMargins(0, 0, 0, 0)      
        self.lay.addWidget(self.plotWidget)

        # add toolbar
        self.addToolBar(QtCore.Qt.BottomToolBarArea, NavigationToolbar(self.plotWidget, self))        

        # button event
        self.pushButton.clicked.connect(self.update)

        # show window
        self.show() 

    #############################################     

    def update(self):

        # clear local axis
        self.ax1.cla()   

        # get axis from analysis object
        a = Analysis()        
        ax = a.getAxis()

        # ???????????
        self.ax1 = ax

        # draw the new content
        self.fig.canvas.draw_idle()  



if __name__ == '__main__':

    app = QtWidgets.QApplication(sys.argv)
    window = MyWindow()
    sys.exit(app.exec_())

这个想法是在Analysis类中创建一个matplotlib对象,并存储结果轴以另一种方法将其绘制到嵌入式PyQt5小部件上。

但是我的代码不起作用。它清除局部轴,但不设置Analysis对象的轴。我该怎么做呢?

谢谢!

1 个答案:

答案 0 :(得分:2)

您正在课程Analysis中创建一个新的图形和坐标轴。您不能像这样从一个图形中选取一个轴并将其放在另一图形中。相反,请设计您的Analysis类,以便在初始化时使用Axes对象。

您可能想阅读matplotlib的coding style document

class Analysis():
    def __init__(self, ax=None):
        if ax is None:
            ax = plt.gca()

        bins = np.arange(0.1, 2, 0.02)
        data = np.random.rand(3,2)        
        ax.hist(data, bins, alpha=0.6, density=False, cumulative=False)

(...)

    def update(self):

        # clear local axis
        self.ax1.cla()   
        a = Analysis(ax=self.ax1)        
        self.fig.canvas.draw_idle()