简体   繁体   中英

What event is associated with zooming an interactive matplotlib plot?

As I understand it, when a user interacts with an interactive matplotlib plot (ie by clicking, pressing a key, etc.), an Event is triggered, which can be linked to an arbitrary callback function , if desired.

Interactive matplotlib plots often come with a navigation toolbar that includes certain features like zooming and rubberband selection. My question is, is there a way to watch for these things from the backend and react when a user performs one of these actions using the nav bar/mouse?

I have gone through the list of event names on theevent handling page of matplotlib's documentation, as well as looked over the API reference for the NavigationToolbar2 class, but I haven't been able to find any connection between the two. Is an event even the thing to be looking for, or is there some other way to detect these kinds of interactions?

Resolved on my own. In addition to the event types and the fig.canvas.mpl_connect() syntax shown on the "event handling" documentation page , you can also associate a callback function with an Axes instance directly, and this way has some different kinds of events that can be used as triggers. The API reference for the Axes class has this to say:

The Axes instance supports callbacks through a callbacks attribute which is a CallbackRegistry instance. The events you can connect to are 'xlim_changed' and 'ylim_changed' and the callback will be called with func(ax) where ax is the Axes instance.

...and then the syntax to connect these axis events to a user-defined callback func on an existing axis instance ax might look something like this:

def func(axes):
    print("New axis y-limits are", axes.get_ylim())

cb_registry = ax.callbacks
cid = cb_registry.connect('ylim_changed', func)

The same approach could be used to watch for x-axis changes as well.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM