mpl-extensions / mpl-interactions

Sliders to control matplotlib and other interactive goodies. Works in any interactive backend and even uses ipywidgets when in a Jupyter notebook
https://mpl-interactions.rtfd.io
BSD 3-Clause "New" or "Revised" License
132 stars 20 forks source link

panhandler not working when plot embedded in pyqt5 #277

Closed ruixunbao closed 1 year ago

ruixunbao commented 1 year ago

Bug report

Bug summary

When a plot embedded in pyqt5, panhandler does not work anymore while zoom_factory still works.

Code for reproduction


from mpl_interactions import ioff, panhandler, zoom_factory
import sys
import matplotlib
matplotlib.use('Qt5Agg')

from PyQt5 import QtCore, QtWidgets

from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg
from matplotlib.figure import Figure

class MplCanvas(FigureCanvasQTAgg):

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

class MainWindow(QtWidgets.QMainWindow):

    def __init__(self, *args, **kwargs):
        super(MainWindow, self).__init__(*args, **kwargs)

        # Create the maptlotlib FigureCanvas object,
        # which defines a single set of axes as self.axes.
        sc = MplCanvas(self, width=5, height=4, dpi=100)
        sc.axes.plot([0,1,2,3,4], [10,1,20,3,40])
        self.setCentralWidget(sc)

        self.show()

app = QtWidgets.QApplication(sys.argv)
w = MainWindow()
app.exec_()

Version Info

ianhi commented 1 year ago

Hi @ruixunbao i take it you got it wroking?

The critical part is to assign it to a long lived variable like so: self._ph = panhandler(fig)

Also

I'm trying to slowly transition the panhandler usage over to a dedicated zooming and panning package mpl-pan-zoom (docs here: https://mpl-pan-zoom.readthedocs.io/en/latest/) which I'd reocmmend switching too as I plan to deprecate the usage from mpl-interactions

ruixunbao commented 1 year ago

Hi @ruixunbao i take it you got it wroking?

The critical part is to assign it to a long lived variable like so: self._ph = panhandler(fig)

Also

I'm trying to slowly transition the panhandler usage over to a dedicated zooming and panning package mpl-pan-zoom (docs here: https://mpl-pan-zoom.readthedocs.io/en/latest/) which I'd reocmmend switching too as I plan to deprecate the usage from mpl-interactions

Thanks! @ianhi. I found the solution in [https://github.com/mpl-extensions/mpl-interactions/issues/228#issuecomment-1003829856] #228 . I've switched to package mpl-pan-zoom. I really appreciate the work you've done.