0

I have a window that I made with PyQT designer which looks like this: enter image description here

I want to plot a graph using matplotlib inside the left frame, in the red box. How do I do that? I have created a method called plot_graph() that I want to plot, and have tried to call the method.

I also have an additional question, I have pyqt6 and pyqt5 installed. Do I have to use matplotlib.backends.backend_qt5agg? I would like to use matplotlib.backends.backend_qt6agg, but this does not seem to exist.

Below is my code:

from PyQt6 import QtCore, QtGui, QtWidgets
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg
from matplotlib.backends.backend_qt5agg import NavigationToolbar2QT as NavigationToolbar
from matplotlib.figure import Figure
import matplotlib.pyplot as plt
import numpy as np

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(800, 600)
        self.centralwidget = QtWidgets.QWidget(parent=MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.horizontalLayout = QtWidgets.QHBoxLayout(self.centralwidget)
        self.horizontalLayout.setObjectName("horizontalLayout")
        self.left_frame = QtWidgets.QFrame(parent=self.centralwidget)
        self.left_frame.setFrameShape(QtWidgets.QFrame.Shape.StyledPanel)
        self.left_frame.setFrameShadow(QtWidgets.QFrame.Shadow.Raised)
        self.left_frame.setObjectName("left_frame")
        self.verticalLayout_2 = QtWidgets.QVBoxLayout(self.left_frame)
        self.verticalLayout_2.setObjectName("verticalLayout_2")
        self.left_label = QtWidgets.QLabel(parent=self.left_frame)
        self.left_label.setObjectName("left_label")
        self.verticalLayout_2.addWidget(self.left_label, 0, QtCore.Qt.AlignmentFlag.AlignTop)
        self.horizontalLayout.addWidget(self.left_frame)
        self.right_frame = QtWidgets.QFrame(parent=self.centralwidget)
        self.right_frame.setFrameShape(QtWidgets.QFrame.Shape.StyledPanel)
        self.right_frame.setFrameShadow(QtWidgets.QFrame.Shadow.Raised)
        self.right_frame.setObjectName("right_frame")
        self.verticalLayout = QtWidgets.QVBoxLayout(self.right_frame)
        self.verticalLayout.setObjectName("verticalLayout")
        self.right_label = QtWidgets.QLabel(parent=self.right_frame)
        self.right_label.setObjectName("right_label")
        self.verticalLayout.addWidget(self.right_label, 0, QtCore.Qt.AlignmentFlag.AlignTop)
        self.horizontalLayout.addWidget(self.right_frame)
        MainWindow.setCentralWidget(self.centralwidget)
        self.statusbar = QtWidgets.QStatusBar(parent=MainWindow)
        self.statusbar.setObjectName("statusbar")
        
        # setup matplotlib
        self.figure = Figure()
        self.canvas = FigureCanvasQTAgg(self.figure)
        self.plot_graph()

        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.left_label.setText(_translate("MainWindow", "Left Frame"))
        self.right_label.setText(_translate("MainWindow", "Right Frame"))

    def plot_graph(self):
        t = np.arange(0.0, 2.0, 0.01)
        s = 1 + np.sin(2 * np.pi * t)

        fig, ax = plt.subplots()
        ax.plot(t, s)

        ax.set(xlabel='time (s)', ylabel='voltage (mV)',
            title='About as simple as it gets, folks')
        ax.grid()

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())
zampoan
  • 57
  • 7
  • The solution is simple: do **not** edit pyuic generated files (as the file header comment, which you ignored and removed, clearly warns about). Any custom logic **must** be placed in the actual main script of that UI (or program) as explained in the official guidelines about [using Designer](//www.riverbankcomputing.com/static/Docs/PyQt5/designer.html). You can also add custom widgets to Designer too, using promoted widgets (see [this post](https://stackoverflow.com/q/45872255) for pyqtgraph, and [this one](https://stackoverflow.com/a/60311724) to understand how *widget promotion* works). – musicamante May 08 '23 at 01:44

0 Answers0