matplotlib.pyplot.connect#

matplotlib.pyplot.connect(s, func)[源代码][源代码]#

将函数 func 绑定到事件 s

参数:
sstr

以下事件ID之一:

  • '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'.

函数可调用

要执行的回调函数,该函数必须具有以下签名:

def func(event: Event) -> Any

对于位置事件(按钮和按键的按下/释放),如果鼠标在 Axes 上,事件的 inaxes 属性将被设置为事件发生的 Axes,此外,变量 xdataydata 属性将被设置为鼠标在数据坐标中的位置。更多信息请参见 KeyEventMouseEvent

备注

如果 func 是一个方法,这只会存储对该方法的弱引用。因此,图形不会影响相关对象的生命周期。通常,您希望通过保持对它的引用来确保对象在整个图形生命周期内保持活动状态。

返回:
cid

一个可以与 FigureCanvasBase.mpl_disconnect 一起使用的连接ID。

注释

示例

def on_press(event):
    print('you pressed', event.button, event.xdata, event.ydata)

cid = canvas.mpl_connect('button_press_event', on_press)

使用 matplotlib.pyplot.connect 的示例#

鼠标移动和点击事件

Mouse move and click events