I have an application that has worked for over a year. I added a new section. This section is opens a dialog upon pressing a button. In this dialog, I put a bland QWidget. I want to plot using matplotlib on that widget. Below I will show what I tried to use as a basis. I am not able to get this task done. Many of the example I took did not work at all - i.e. when I run the application, I get messages that methods do not exist. There were cases where I fixed those programming errors and then the plot opened in another window but not where I wanted it to open. Is there a humane way to put a matplotlib plot on a qwidget? If not, what should I do to get a plot there. I'm coming close to declaring this cannot be done. Please point me in the correct direction. I am trying to do this in python3 - not in python2 - the way the examples are written could have been from python2 and perhaps the examples are out dated.
I looked at
1- Plotting matplotlib figure inside QWidget using Qt Designer form and PyQt5. Note, I am using pyside2 and not qt5 directly.
2- https://www.pythonguis.com/tutorials/pyside-plotting-matplotlib/
Note- My qwidget is not the "central widget" it is a specific widget that I defined in the qt-designer at a specific place. In the end there will be several like this.
I expected that I would figure out a way to plot in the place that I wanted to plot. In the end, this did not work. Either I got errors or the plot was plotted in a new window that was opened up.
This code: I edited the code - sorry for the prints, it's my way to know where I got:
from PySide2.QtWidgets import *
import PySide2.QtWidgets as QtWidgets
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as Canvas
from matplotlib.figure import Figure
import matplotlib
matplotlib.use('Qt5Agg')
class MplCanvas(Canvas):
'''
Class to represent the FigureCanvas widget
'''
def __init__(self):
print('7')
self.fig = Figure()
print('8')
self.ax = self.fig.add_subplot(111)
print('9')
super(MplCanvas, self).__init__(self.fig)
print('10')
#Canvas.setSizePolicy(self, QSizePolicy.Expanding, QSizePolicy.Expanding)
print('11')
Canvas.updateGeometry(self)
print('12')
class MplWidget(QWidget):
'''
Widget promoted and defined in Qt Designer
'''
def __init__(self, parent=None):
#QWidget.__init__(self, parent)
super(MplWidget, self).__init__(parent)
print('1')
self.vbl = QVBoxLayout()
print('2')
#self.canvas = Canvas(Figure())
print('3')
self.canvas = MplCanvas()
print('3.5')
self.vbl.addWidget(self.canvas)
#self.vbl.addWidget(parent)
print('4')
self.setLayout(self.vbl)
print('5')
self.checkCanvas()
print('5')
def checkCanvas(self):
print(self.canvas)
def getCanvas(self):
return self.canvas
When I run just the constuctor: plotWidget = MplWidget(self.ink_limit_master_dialog.pyplot_widget)
I print:
1
2
3
7
8
9
10
11
12
3.5
But Then I get the following errors:
Traceback (most recent call last):
File "C:/Work/ResourceGenerator/ResCreatorNewGeneration/res_create_NewGen_uic.py", line 1109, in ink_limit_master_callback
plotWidget = MplWidget(self.ink_limit_master_dialog.pyplot_widget)
File "C:\Work\ResourceGenerator\ResCreatorNewGeneration\MplWidgetClass.py", line 70, in __init__
self.vbl.addWidget(self.canvas)
TypeError: 'PySide2.QtWidgets.QBoxLayout.addWidget' called with wrong argument types:
PySide2.QtWidgets.QBoxLayout.addWidget(MplCanvas)
Supported signatures:
PySide2.QtWidgets.QBoxLayout.addWidget(PySide2.QtWidgets.QWidget, int = 0, PySide2.QtCore.Qt.Alignment = Default(Qt.Alignment))
PySide2.QtWidgets.QBoxLayout.addWidget(PySide2.QtWidgets.QWidget)
Basically I can't add the MplCanvas as a widget.
Seems it is not trivial to attach matplotlib to a qwidget...
_init_with single underscores instead of__init__, so what you wrote there is never run at all. You should also avoid calling the class methods with the instance, and properly usesuper()or the function as actual instance methods: for instance,super().__init__(self.fig),super().__init__(parent)orself.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding).from PySide2 import QtCoreat the very beginning of your code (or, at least, before any matplotlib import). See the Qt Bindings docs on matplotlib.