I am trying to create a UI, which has several subplots as outputs. Then when they one of them is clicked it zooms in on the plot, and when you click again it zoom out.
For this I use the following code:
class ZoomingSubplots(object):
def __init__(self, fig, *args, **kwargs):
self.fig, self.axes = fig, fig.subplots(*args, **kwargs)
self._zoomed = False
self.fig.canvas.mpl_connect('button_press_event', self.on_click)
def zoom(self, selected_ax):
for ax in self.axes.flat:
ax.set_visible(False)
self._original_size = selected_ax.get_position()
selected_ax.set_position([0.05, 0.07, 0.90, 0.80])
selected_ax.set_visible(True)
self._zoomed = True
def unzoom(self, selected_ax):
selected_ax.set_position(self._original_size)
for ax in self.axes.flat:
ax.set_visible(True)
self._zoomed = False
def on_click(self, event):
if event.inaxes is None:
return
if self._zoomed:
self.unzoom(event.inaxes)
else:
self.zoom(event.inaxes)
self.fig.canvas.draw()
And this works great. However, one thing I don't like is that this will happen even if it is a right-click on the mouse. Is there any way to only make this happen when the left mouse button is clicked, or...?
event.buttonis 1 (mouse left button) to determine whether to perform the zooming.