简体   繁体   English

在pyqt中嵌入Matplotlib画布的实例-图未显示

[英]Embedd instance of Matplotlib canvas in pyqt - plot not showing up

I tried to keep the example as short as possible. 我试图使示例尽可能简短。 There is a similar question on SO . 关于SO也有类似的问题 In contrast to the answer provided in the mentioned question, I would like to define a MatplotlibFigure class so that I can reuse the code. 与上述问题提供的答案相反,我想定义一个MatplotlibFigure类,以便可以重用代码。 I added a reference to the instance of Figure as self.figure = MatplotlibFigure() in the function make_tab1 and added self.figure to the splitter . 我添加到图的实例的引用作为self.figure = MatplotlibFigure()在功能make_tab1并加入self.figuresplitter The pushbutton seems to work because I can save the figure using the navigation toolbar. 该按钮似乎起作用,因为我可以使用导航工具栏保存该图。 However, the axis are not shown in the GUI. 但是,该轴未在GUI中显示。

Why are the axis not visible ? 为什么轴不可见?

import sys
from PyQt5.QtWidgets import QMainWindow, QApplication, QPushButton, QWidget, QAction, QTabWidget, QVBoxLayout,\
                            QHBoxLayout, QGridLayout, QSplitter, QFrame, QTextEdit, QFileDialog, QGroupBox,\
                            QButtonGroup, QLabel, QRadioButton

from PyQt5 import QtCore

import matplotlib
from matplotlib import figure
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.backends.backend_qt5agg import NavigationToolbar2QT as NavigationToolbar

class MatplotlibFigure(QWidget):

    # constructor
    def __init__(self):
        super().__init__()
        #self.layout = QBoxLayout()
        self.figure = matplotlib.figure.Figure()
        self.canvas = FigureCanvas(self.figure)
        self.toolbar = NavigationToolbar(self.canvas, self)
        #self.layout.addWidget(self.canvas)

    def plot(self):
        self.figure.clf()
        ax = self.figure.add_subplot(111)
        x = [i for i in range(100)]
        y = [i**0.5 for i in x]
        ax.plot(x, y, 'g*-')
        self.canvas.draw_idle()
        print('PLOTTED')


class MainApp(QMainWindow):
    """ This creates the main Window and configures its look
    """
    def __init__(self):
        '''
        Initializes the class with standard parameters
        '''
        super().__init__()
        self.title = 'Data Analysis App'

        # start at top left corner of the screen
        self.left = 0
        self.top = 0

        # set the window height and width
        self.width = 1280
        self.height = 780

        # set the styles of fonts etc.
        self.setStyleSheet('font-size: 16pt')

        # call the makeUI method
        self.makeUI()

    def makeUI(self):
        # set window geometry
        self.setWindowTitle(self.title)
        self.setGeometry(self.left, self.top, self.width, self.height)

        # generate the tab layout
        self.table_widget = MyTableWidget()
        self.setCentralWidget(self.table_widget)

        # apparently we have to call the show method in order to show it
        self.show()


class MyTableWidget(QWidget):
    """
    Initializes the tab layout
    """

    def __init__(self):
        super().__init__()
        self.layout = QVBoxLayout(self)

        # initialize the tab screens
        self.tabs = QTabWidget()

        # add the tab screens to the tab widget
        self.tabs.resize(600, 600)

        # make the layout of the tabs
        self.make_tab1()

        # initialize the tabs
        self.layout.addWidget(self.tabs)
        self.setLayout(self.layout)

    def make_tab1(self):
        """Style of tab 1 is created here"""

        '''Defines master layout of tab1'''
        tab1 = QWidget()
        tab1.layout = QHBoxLayout()

        '''Button Section'''

        # converts the data into excel and pandas dataframe
        btn_plot_data = QPushButton('Plot data')
        btn_plot_data.clicked.connect(self.plot_data)


        '''Button layout section'''
        btn_layout = QVBoxLayout()
        btn_layout.addWidget(btn_plot_data)
        btn_layout.addStretch(1)

        '''Page layout section '''

        left = QFrame()
        left.setLayout(btn_layout)

        self.figure = MatplotlibFigure()

        # combine the buttons and the canvas in a splitter layout
        splitter1 = QSplitter(QtCore.Qt.Horizontal)
        splitter1.addWidget(left)
        splitter1.addWidget(self.figure)
        splitter1.setSizes([5, 400])


        # add the last splitter to the layout
        tab1.layout.addWidget(splitter1)
        tab1.setLayout(tab1.layout)
        self.tabs.addTab(tab1, 'Get Data Module')

    ''' Methods section
    '''
    def plot_data(self):
        self.figure.plot()


if __name__ == '__main__':
    """ This code block is always the same and makes sure that e.g. STRG+C kills the window etc.
    """
    app = QApplication(sys.argv)
    ex = MainApp()
    sys.exit(app.exec())

You need to set a parent to FigureCanvas : 您需要将父FigureCanvas设置为FigureCanvas

self.canvas.setParent(self)

在此处输入图片说明

You forgot to set a layout for your MatplotlibFigure class (remember that it is a QWidget in your case, not a canvas) 您忘记为MatplotlibFigure类设置布局(请记住,在您的情况下,它是QWidget,而不是画布)

def __init__(self):
    super().__init__()
    #self.layout = QBoxLayout()
    self.figure = matplotlib.figure.Figure()
    self.canvas = FigureCanvas(self.figure)
    self.toolbar = NavigationToolbar(self.canvas, self)

    layout = QVBoxLayout(self)
    layout.addWidget(self.toolbar)
    layout.addWidget(self.canvas)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM