Reputation: 2012
I have an application with several windows, where each window has their own Matplotlib canvas. I've set up a PlotCanvas
for the drawing, and a WidgetPlot
wrapper that decides the GUI layout and allows hooking into the backend for e.g. shortcuts.
Anyway, I would like for each window to instantiate their canvas with
self.m = WidgetPlot(ax_layout = "triple") # <- How do I make sure this argument reaches PlotCanvas?
self.canvas = self.m.canvas
self.mpl_LayoutBox.addWidget(self.m)
But as I've pointed out in the comment, I'm too stupid at OOP to figure out how to pass e.g. "ax_layout
" as an argument for instantiating different types of plot layouts.
Minimal example below where I've tried to set ax_layout
. Working example obtained if you delete the ax_layout
arguments and instead set self.ax_layout = "single"
directly in the PlotCanvas
class.
from PyQt5.Qt import *
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.figure import Figure
class WidgetPlot(QWidget):
def __init__(self, ax_layout, *args, **kwargs):
QWidget.__init__(self, *args, **kwargs)
self.setLayout(QVBoxLayout())
self.canvas = PlotCanvas(self, ax_layout)
self.layout().addWidget(self.canvas)
class PlotCanvas(FigureCanvas):
def __init__(self, ax_layout, parent = None, width = 1, height = 1, dpi = 100):
self.fig = Figure(figsize = (width, height), dpi = dpi, tight_layout = True)
self.ax_layout = ax_layout
if self.ax_layout == "triple":
self.ax1 = self.fig.add_subplot(131)
self.ax2 = self.fig.add_subplot(132)
self.ax3 = self.fig.add_subplot(133)
self.ax1.plot(range(100), color = "green")
self.ax2.plot(range(100), color = "red")
self.ax3.plot(range(100), color = "blue")
elif self.ax_layout == "single":
self.ax = self.fig.add_subplot(111)
self.ax.plot(range(100), color = "black")
else:
raise ValueError
FigureCanvas.__init__(self, self.fig)
class Ui_TraceWindow(object):
def setupUi(self, TraceWindow):
TraceWindow.setObjectName("TraceWindow")
TraceWindow.resize(1086, 500)
TraceWindow.setMinimumSize(QSize(900, 500))
TraceWindow.setMaximumSize(QSize(5000, 2000))
self.centralWidget = QWidget(TraceWindow)
self.centralWidget.setObjectName("centralWidget")
self.gridLayout_2 = QGridLayout(self.centralWidget)
self.gridLayout_2.setContentsMargins(11, 11, 11, 11)
self.gridLayout_2.setSpacing(6)
self.gridLayout = QGridLayout()
self.gridLayout.setSpacing(6)
self.mpl_LayoutBox = QVBoxLayout()
self.mpl_LayoutBox.setSpacing(6)
self.gridLayout.addLayout(self.mpl_LayoutBox, 3, 1, 1, 1)
self.gridLayout_2.addLayout(self.gridLayout, 1, 0, 1, 1)
TraceWindow.setCentralWidget(self.centralWidget)
# Instantiate canvas here
self.m = WidgetPlot(ax_layout = "triple") # <- How do I make sure this argument reaches PlotCanvas?
self.canvas = self.m.canvas
self.mpl_LayoutBox.addWidget(self.m)
if __name__ == "__main__":
import sys
app = QApplication(sys.argv)
TraceWindow = QMainWindow()
ui = Ui_TraceWindow()
ui.setupUi(TraceWindow)
TraceWindow.show()
sys.exit(app.exec_())
Upvotes: 0
Views: 85
Reputation: 244162
An additional parameter must be to the right of other parameters, it would also be advisable to have a default value:
from PyQt5.Qt import *
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.figure import Figure
class WidgetPlot(QWidget):
def __init__(self, parent=None, ax_layout=""):
QWidget.__init__(self, parent)
self.setLayout(QVBoxLayout())
self.canvas = PlotCanvas(self, ax_layout=ax_layout)
self.layout().addWidget(self.canvas)
class PlotCanvas(FigureCanvas):
def __init__(self, parent = None, width = 1, height = 1, dpi = 100, ax_layout=""):
self.fig = Figure(figsize = (width, height), dpi = dpi, tight_layout = True)
self.ax_layout = ax_layout
if self.ax_layout == "triple":
self.ax1 = self.fig.add_subplot(131)
self.ax2 = self.fig.add_subplot(132)
self.ax3 = self.fig.add_subplot(133)
self.ax1.plot(range(100), color = "green")
self.ax2.plot(range(100), color = "red")
self.ax3.plot(range(100), color = "blue")
elif self.ax_layout == "single":
self.ax = self.fig.add_subplot(111)
self.ax.plot(range(100), color = "black")
else:
raise ValueError
FigureCanvas.__init__(self, self.fig)
class Ui_TraceWindow(object):
def setupUi(self, TraceWindow):
TraceWindow.setObjectName("TraceWindow")
TraceWindow.resize(1086, 500)
TraceWindow.setMinimumSize(QSize(900, 500))
TraceWindow.setMaximumSize(QSize(5000, 2000))
self.centralWidget = QWidget(TraceWindow)
self.centralWidget.setObjectName("centralWidget")
self.gridLayout_2 = QGridLayout(self.centralWidget)
self.gridLayout_2.setContentsMargins(11, 11, 11, 11)
self.gridLayout_2.setSpacing(6)
self.gridLayout = QGridLayout()
self.gridLayout.setSpacing(6)
self.mpl_LayoutBox = QVBoxLayout()
self.mpl_LayoutBox.setSpacing(6)
self.gridLayout.addLayout(self.mpl_LayoutBox, 3, 1, 1, 1)
self.gridLayout_2.addLayout(self.gridLayout, 1, 0, 1, 1)
TraceWindow.setCentralWidget(self.centralWidget)
# Instantiate canvas here
self.m = WidgetPlot(ax_layout = "triple") # <- How do I make sure this argument reaches PlotCanvas?
self.canvas = self.m.canvas
self.mpl_LayoutBox.addWidget(self.m)
if __name__ == "__main__":
import sys
app = QApplication(sys.argv)
TraceWindow = QMainWindow()
ui = Ui_TraceWindow()
ui.setupUi(TraceWindow)
TraceWindow.show()
sys.exit(app.exec_())
Upvotes: 1