I found the solution through the help of this SO post, which links to this free sample chapter of a book I’m probably gonna need to buy. As sort of mentioned in a number of other posts, one needs to “Promote” the blank QtWidget to a MplWidget using the header mplwidget. After doing this, and then running the pyuic5 command, “from mplwidget import MplWidget” will appear in the design.py file which can be updated anytime without worry about overwritting. Then, create an mplwidget.py file with:
# Imports
from PyQt5 import QtWidgets
from matplotlib.figure import Figure
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as Canvas
import matplotlib
# Ensure using PyQt5 backend
matplotlib.use('QT5Agg')
# Matplotlib canvas class to create figure
class MplCanvas(Canvas):
def __init__(self):
self.fig = Figure()
self.ax = self.fig.add_subplot(111)
Canvas.__init__(self, self.fig)
Canvas.setSizePolicy(self, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Expanding)
Canvas.updateGeometry(self)
# Matplotlib widget
class MplWidget(QtWidgets.QWidget):
def __init__(self, parent=None):
QtWidgets.QWidget.__init__(self, parent) # Inherit from QWidget
self.canvas = MplCanvas() # Create canvas object
self.vbl = QtWidgets.QVBoxLayout() # Set box for plotting
self.vbl.addWidget(self.canvas)
self.setLayout(self.vbl)
Then, the app framework can be as I had earlier. When run, and pushing the plot button, the figure appears as expected. I think I basically had done everything to “Promote” the QWidget by hand just missing the vbl stuff, but all that would be overwritten everytime the Qt Designer form is editted.
app_framework.py:
from PyQt5.QtWidgets import QApplication, QMainWindow
import design
class MyApp(QMainWindow, design.Ui_mainWindow):
def __init(self):
super(self.__class__, self).__init__()
self.setupUi(self)
self.pushButton_plotData.clicked.connect(self.plot_data)
def plot_data(self):
x=range(0, 10)
y=range(0, 20, 2)
self.plotWidget.canvas.ax.plot(x, y)
self.plotWidget.canvas.draw()
main.py:
from PyQt5 import QtWidgets
import sys
# Local Module Imports
import app_framework as af
# Create GUI application
app = QtWidgets.QApplication(sys.argv)
form = af.MyApp()
form.show()
app.exec_()