图形/坐标轴进入和离开事件

图形/坐标轴进入和离开事件#

通过在进入和离开时改变帧颜色来说明图形和坐标轴的进入和离开事件。

# %matplotlib tk
import matplotlib.pyplot as plt


def on_enter_axes(event):
    print('enter_axes', event.inaxes)
    event.inaxes.patch.set_facecolor('yellow')
    event.canvas.draw()


def on_leave_axes(event):
    print('leave_axes', event.inaxes)
    event.inaxes.patch.set_facecolor('white')
    event.canvas.draw()


def on_enter_figure(event):
    print('enter_figure', event.canvas.figure)
    event.canvas.figure.patch.set_facecolor('red')
    event.canvas.draw()


def on_leave_figure(event):
    print('leave_figure', event.canvas.figure)
    event.canvas.figure.patch.set_facecolor('grey')
    event.canvas.draw()


fig, axs = plt.subplots(2, 1)
fig.suptitle('mouse hover over figure or Axes to trigger events')

fig.canvas.mpl_connect('figure_enter_event', on_enter_figure)
fig.canvas.mpl_connect('figure_leave_event', on_leave_figure)
fig.canvas.mpl_connect('axes_enter_event', on_enter_axes)
fig.canvas.mpl_connect('axes_leave_event', on_leave_axes)

plt.show()
enter_figure Figure(640x480)
enter_axes Axes(0.125,0.53;0.775x0.35)
leave_axes Axes(0.125,0.53;0.775x0.35)
enter_axes Axes(0.125,0.11;0.775x0.35)
leave_axes Axes(0.125,0.11;0.775x0.35)
enter_axes Axes(0.125,0.53;0.775x0.35)
leave_axes Axes(0.125,0.53;0.775x0.35)
enter_axes Axes(0.125,0.11;0.775x0.35)
leave_axes Axes(0.125,0.11;0.775x0.35)
enter_axes Axes(0.125,0.53;0.775x0.35)
leave_axes Axes(0.125,0.53;0.775x0.35)
enter_axes Axes(0.125,0.11;0.775x0.35)
leave_axes Axes(0.125,0.11;0.775x0.35)
enter_axes Axes(0.125,0.53;0.775x0.35)
leave_axes Axes(0.125,0.53;0.775x0.35)
enter_axes Axes(0.125,0.11;0.775x0.35)
leave_axes Axes(0.125,0.11;0.775x0.35)
enter_axes Axes(0.125,0.53;0.775x0.35)
leave_axes Axes(0.125,0.53;0.775x0.35)
enter_axes Axes(0.125,0.11;0.775x0.35)
leave_axes Axes(0.125,0.11;0.775x0.35)
enter_axes Axes(0.125,0.53;0.775x0.35)
leave_axes Axes(0.125,0.53;0.775x0.35)
enter_axes Axes(0.125,0.11;0.775x0.35)
leave_axes Axes(0.125,0.11;0.775x0.35)
enter_axes Axes(0.125,0.53;0.775x0.35)
leave_axes Axes(0.125,0.53;0.775x0.35)
enter_axes Axes(0.125,0.11;0.775x0.35)
leave_axes Axes(0.125,0.11;0.775x0.35)
leave_figure Figure(640x480)