matplotlib.pyplot.connect#

matplotlib.pyplot.connect(s,func)[source]#

Bind functionfunc to events.

Parameters:
sstr

One of the following events ids:

  • 'button_press_event'

  • 'button_release_event'

  • 'draw_event'

  • 'key_press_event'

  • 'key_release_event'

  • 'motion_notify_event'

  • 'pick_event'

  • 'resize_event'

  • 'scroll_event'

  • 'figure_enter_event',

  • 'figure_leave_event',

  • 'axes_enter_event',

  • 'axes_leave_event'

  • 'close_event'.

funccallable

The callback function to be executed, which must have thesignature:

deffunc(event:Event)->Any

For the location events (button and key press/release), if themouse is over the Axes, theinaxes attribute of the event willbe set to theAxes the event occurs is over, andadditionally, the variablesxdata andydata attributes willbe set to the mouse location in data coordinates. SeeKeyEventandMouseEvent for more info.

Note

If func is a method, this only stores a weak reference to themethod. Thus, the figure does not influence the lifetime ofthe associated object. Usually, you want to make sure that theobject is kept alive throughout the lifetime of the figure byholding a reference to it.

Returns:
cid

A connection id that can be used withFigureCanvasBase.mpl_disconnect.

Notes

Examples

defon_press(event):print('you pressed',event.button,event.xdata,event.ydata)cid=canvas.mpl_connect('button_press_event',on_press)

Examples usingmatplotlib.pyplot.connect#

Mouse move and click events

Mouse move and click events