0

I am working on PyQt application that displays 2 FigureCanvasQTAgg inside of a QWidget. Currently, they are both linked to separate NavigationToolbar2QT objects but I would like to be able to control both of them from the same toolbar so that any navigation that occurs inside of one canvas is mirrored in the other canvas.

Here is a watered down version of the application:

from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg, NavigationToolbar2QT
from PyQt5.QtWidgets import *

class MplCanvas(FigureCanvasQTAgg):
    def __init__(self, parent=None, width=5, height=4, dpi=100):
        fig = Figure(figsize=(width, height), dpi=dpi)
        self.axes = fig.add_subplot(111)
        super(MplCanvas, self).__init__(fig)

class GraphDisplay(QWidget):
    def __init__(self):
        super().__init__()
        self.main_layout = QVBoxLayout(self)
        self.toolbar_layout = QHBoxLayout()
        self.canvas_layout = QHBoxLayout()

        self.left_canvas = MplCanvas()
        self.left_toolbar = NavigationToolbar2QT(self.left_canvas, self)
        self.right_canvas = MplCanvas()
        self.right_toolbar = NavigationToolbar2QT(self.right_canvas, self)
        
        self.toolbar_layout.addWidget(self.left_toolbar)
        self.toolbar_layout.addWidget(self.right_toolbar)
        self.canvas_layout .addWidget(self.left_canvas)
        self.canvas_layout .addWidget(self.right_canvas)

        self.main_layout.addLayout(self.toolbar_layout)
        self.main_layout.addLayout(self.canvas_layout)

I've tried looking through the implementation for NavigationToolbar2 but it seems like the object is designed to only be linked to a single canvas. My best guess for how to get the desired behavior would be to still use 2 toolbars and have them linked somehow.

1
  • I'd suggest you to check the source code of NavigationToolbar2QT (it should be somewhere in the backends directory of the matplotlib installation folder. Then study how its function works (especially those using its self.canvas) and create a subclass that overrides those functions and, depending on the case, imitates the original one by also applying their result to the other canvas. Commented Jul 2 at 18:59

0

Browse other questions tagged or ask your own question.