ipyoverlay.mpl.event.MPLEventHandler#

class ipyoverlay.mpl.event.MPLEventHandler(axes, callback, points_x, points_y, tolerance=0.5, button=1)#

Bases: object

Class that attaches a custom event handler to a matplotlib axes, to call a custom callback function only when one of the requested points is clicked on.

This is intended to simplify creating overlay widgets based on clicking within an mpl plot, and abstracts functionality I keep having to reimplement.

TODO: example

Methods

__init__(axes, callback, points_x, points_y)

connect()

Register the event handler and start listening for events.

disconnect()

Unregister this callback from the figure canvas and stop getting events.

Attributes

button

Which mouse button to listen for (by default left click.) Expects the matplotlib.backend_bases.MouseButton enum (for ease: 1 = left, 2 = middle, 3 = right)

callback_id

Once attached, this is the cid of the callback once mpl_connect is called, used to disconnect this handler from the figure.

tolerance

How far away from a point a click will still register as having clicked on that point.

axes

The axes this event will be registered to.

callback

The function to call when a point click is detected.

points_x

The x-data of points to listen for a click at, should likely correspond to the data plotted in the figure.

points_y

The y-data of points to listen for a click at, should likely correspond to the data plotted in the figure.

Parameters:
  • axes (Axes) –

  • callback (Callable) –

  • points_x (ndarray) –

  • points_y (ndarray) –

  • tolerance (float) –

  • button (int) –

axes: Axes#

The axes this event will be registered to. This filters out events from other axes within the same figure, use a separate event handler to listen to each desired axes.

button: int = 1#

Which mouse button to listen for (by default left click.) Expects the matplotlib.backend_bases.MouseButton enum (for ease: 1 = left, 2 = middle, 3 = right)

callback: Callable#

The function to call when a point click is detected. This function will be passed the index within the points_x/points_y that was nearest and the mpl event object.

callback_id: int = None#

Once attached, this is the cid of the callback once mpl_connect is called, used to disconnect this handler from the figure. (Call disconnect()).

connect()#

Register the event handler and start listening for events.

disconnect()#

Unregister this callback from the figure canvas and stop getting events.

points_x: ndarray#

The x-data of points to listen for a click at, should likely correspond to the data plotted in the figure.

points_y: ndarray#

The y-data of points to listen for a click at, should likely correspond to the data plotted in the figure.

tolerance: float = 0.5#

How far away from a point a click will still register as having clicked on that point.