Display a table beside a plot in PyQt5

2019-08-24 16:51发布

问题:

I am new at using PyQt and this is my first GUI application. I have a challenge trying to display a table beside a plot in iterations. Please find below my code:

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
%matplotlib qt5
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.backends.backend_qt5agg import NavigationToolbar2QT as NavigationToolbar
from matplotlib.figure import Figure

from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_MainWindow(QtWidgets.QWidget):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(800, 600)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.gridLayout = QtWidgets.QGridLayout(self.centralwidget)
        self.gridLayout.setObjectName("gridLayout")
        self.scrollArea = QtWidgets.QScrollArea(self.centralwidget)
        self.scrollArea.setWidgetResizable(True)
        self.scrollArea.setObjectName("scrollArea")
        self.scrollAreaWidgetContents = QtWidgets.QWidget()
        self.scrollAreaWidgetContents.setGeometry(QtCore.QRect(0, 0, 780, 533))
        self.scrollAreaWidgetContents.setObjectName("scrollAreaWidgetContents")
        self.gridLayout_2 = QtWidgets.QGridLayout(self.scrollAreaWidgetContents)
        self.gridLayout_2.setContentsMargins(0, 0, 0, 0)
        self.gridLayout_2.setObjectName("gridLayout_2")
        self.scene = QtWidgets.QGraphicsScene(self)
        self.graphicsView = QtWidgets.QGraphicsView(self.scrollAreaWidgetContents)
        self.graphicsView.setObjectName("graphicsView")
        self.graphicsView.setScene(self.scene)
        self.gridLayout_2.addWidget(self.graphicsView, 0, 0, 1, 1)

        self.figure = Figure()
        self.canvas = FigureCanvas(self.figure)
        self.scene.addWidget(self.canvas)

        self.pushButton = QtWidgets.QPushButton(self.scrollAreaWidgetContents)
        self.pushButton.setObjectName("pushButton")
        self.pushButton.clicked.connect(self.vis_plot)
        self.gridLayout_2.addWidget(self.pushButton, 1, 0, 1, 1)
        self.scrollArea.setWidget(self.scrollAreaWidgetContents)
        self.gridLayout.addWidget(self.scrollArea, 0, 0, 1, 1)
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 25))
        self.menubar.setObjectName("menubar")
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        self.pushButton.setText(_translate("MainWindow", "PushButton"))


    def vis_plot(self):
        df = pd.DataFrame(np.random.randn(100,4),columns = list('ABCD'))
        ax1 = self.figure.add_subplot(1,2,1)
        ax2 = self.figure.add_subplot(1,2,2)
        for column in df:
            col = pd.DataFrame(df[column])
            col.plot.hist(ax=ax1, title=str(column))
            col_stat = col.apply([np.min, np.max, np.mean, np.median])
            col_table = col_stat.rename(index={'amin':'Minimum', 'amax':'Maximum','mean':'Mean', 'median':'Median'})
            col_table.reset_index(level=0, inplace=True)
            col_tablerender = col_table.rename(columns={col_table.columns[0]:'Metric', col_table.columns[1]:'Value'})
            ax2.table(cellText = col_tablerender.values,
                     colWidths=[0.25]*len(df[column]),
                     colLabels=col_tablerender.columns,
                     cellLoc='center',
                     loc='left',
                     bbox=[-1,0.7,.6,.3])
            self.canvas.draw()




if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())

This is what I am currently getting:

What I currently have

I would prefer if the table is not plotted but printed beside the corresponding plots like the image below:

What I want

标签: python pyqt5