I am not able to find a working code example for the various NavigationToolbar2 callbacks. I have read through the docs, but am still working my way up the learning curve and am not having any luck locating code examples showing how to properly connect to the events I'm interested in.
For specificity, let's focus only on "How do I attach code to release_zoom()?"
The above link provides this documentation:
release_pan(event) - Callback for mouse button release in pan/zoom mode.
Lines of interest in the (incorrectly) working example below are:
self.nt.release_zoom('button_release_event')
self.canvas.mpl_connect('button_release_event', self.on_rel_zoom1)
self.canvas.mpl_connect('release_zoom', self.on_rel_zoom2)
I only manage to connect to the button_release_event. How do I correctly connect to release_zoom()?
from PyQt5 import QtWidgets
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.backends.backend_qt5agg import NavigationToolbar2QT as NavigationToolbar
from matplotlib.figure import Figure
import numpy as np
class PlotWin(QtWidgets.QMainWindow):
def __init__(self, parent=None):
super(PlotWin, self).__init__(parent)
QtWidgets.QMainWindow.__init__(self, parent)
self.win = QtWidgets.QWidget(self)
self.setCentralWidget(self.win)
layout = QtWidgets.QVBoxLayout(self.win)
self.canvas = FigureCanvas(Figure())
layout.addWidget(self.canvas)
self.nt = NavigationToolbar(self.canvas, self)
layout.addWidget(self.nt)
self.ax1 = self.canvas.figure.add_subplot(111)
self.ax1.plot(np.linspace(1, 100, 100), np.random.rand(100, 1))
self.nt.release_zoom('button_release_event')
self.canvas.mpl_connect('button_release_event', self.on_rel_zoom1)
self.canvas.mpl_connect('release_zoom', self.on_rel_zoom2)
def on_rel_zoom1(self, event):
print('One')
def on_rel_zoom2(self, event):
print('Two')
if __name__ == '__main__':
import sys
if not QtWidgets.QApplication.instance():
app = QtWidgets.QApplication(sys.argv)
else:
app = QtWidgets.QApplication.instance()
window = PlotWin()
window.show()
app.exec_()