matplotlib.pyplot.connect#

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

将函数 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'.

funccallable

要执行的回调函数,其签名必须为

def func(event: Event) -> Any

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

注意

如果 func 是一个方法,这只会存储对该方法的弱引用。因此,图形不会影响关联对象的生命周期。通常,您需要通过持有对该对象的引用来确保其在图形的整个生命周期内保持活跃。

返回:
cid

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

备注

示例

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 的示例#

鼠标移动和点击事件

鼠标移动和点击事件