Skip to content Skip to sidebar Skip to footer

Matplotlib Navigationtoolbar2 Callbacks (e.g. Release_zoom()) Syntax Example?

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 no

Solution 1:

release_pan or release_zoom are callbacks. Those are the functions which are called once the conditions of the user releasing the mouse in pan or zoom mode are satisfied. You cannot connect them, but you may connect to them and of course you may call them if needed.

The possible events to connect are listed in the documentation. They are

events = ['resize_event', 
          'draw_event', 
          'key_press_event', 
          'key_release_event', 
          'button_press_event', 
          'button_release_event', 
          'scroll_event', 
          'motion_notify_event', 
          'pick_event', 
          'idle_event', 
          'figure_enter_event', 
          'figure_leave_event', 
          'axes_enter_event', 
          'axes_leave_event', 
          'close_event']

Since there is no such thing as a "release_zoom_event", this can also not be connected.

But.. you may create this "release_zoom_event" yourself, if needed. This is essentially described in matplotlib hooking in to home/back/forward button events for the "home" button. Adapting that example to the "release_zoom_event" would look like this:

import matplotlib.pyplot as plt
from matplotlib.backend_bases import NavigationToolbar2

release_zoom = NavigationToolbar2.release_zoom

defnew_release_zoom(self, *args, **kwargs):
    s = 'release_zoom_event'
    self.canvas.callbacks.process(s, args[0])
    release_zoom(self, *args, **kwargs)

NavigationToolbar2.release_zoom = new_release_zoom

defhandle_release_zoom(evt):
    print('release_zoom_event')
    print(evt.xdata,evt.ydata)

fig = plt.figure()
fig.canvas.mpl_connect('release_zoom_event', handle_release_zoom)
plt.plot([1,3,1])
plt.show()

Post a Comment for "Matplotlib Navigationtoolbar2 Callbacks (e.g. Release_zoom()) Syntax Example?"