@@ -53,15 +53,19 @@ are sent back to you when the event occurs, and the event descriptions
5353======================= ======================================================================================
5454Event name Class and description
5555======================= ======================================================================================
56- 'button_press_event' :class: `~matplotlib.backend_bases.MouseEvent ` - mouse button is pressed
57- 'button_release_event' :class: `~matplotlib.backend_bases.MouseEvent ` - mouse button is released
58- 'draw_event' :class: `~matplotlib.backend_bases.DrawEvent ` - canvas draw
59- 'key_press_event' :class: `~matplotlib.backend_bases.KeyEvent ` - key is pressed
60- 'key_release_event' :class: `~matplotlib.backend_bases.KeyEvent ` - key is released
61- 'motion_notify_event' :class: `~matplotlib.backend_bases.MouseEvent ` - mouse motion
62- 'pick_event' :class: `~matplotlib.backend_bases.PickEvent ` - an object in the canvas is selected
63- 'resize_event' :class: `~matplotlib.backend_bases.ResizeEvent ` - figure canvas is resized
64- 'scroll_event' :class: `~matplotlib.backend_bases.MouseEvent ` - mouse scroll wheel is rolled
56+ 'button_press_event' :class: `~matplotlib.backend_bases.MouseEvent ` - mouse button is pressed
57+ 'button_release_event' :class: `~matplotlib.backend_bases.MouseEvent ` - mouse button is released
58+ 'draw_event' :class: `~matplotlib.backend_bases.DrawEvent ` - canvas draw
59+ 'key_press_event' :class: `~matplotlib.backend_bases.KeyEvent ` - key is pressed
60+ 'key_release_event' :class: `~matplotlib.backend_bases.KeyEvent ` - key is released
61+ 'motion_notify_event' :class: `~matplotlib.backend_bases.MouseEvent ` - mouse motion
62+ 'pick_event' :class: `~matplotlib.backend_bases.PickEvent ` - an object in the canvas is selected
63+ 'resize_event' :class: `~matplotlib.backend_bases.ResizeEvent ` - figure canvas is resized
64+ 'scroll_event' :class: `~matplotlib.backend_bases.MouseEvent ` - mouse scroll wheel is rolled
65+ 'figure_enter_event' :class: `~matplotlib.backend_bases.LocationEvent ` - mouse enters a new figure
66+ 'figure_leave_event' :class: `~matplotlib.backend_bases.LocationEvent ` - mouse leaves a figure
67+ 'axes_enter_event' :class: `~matplotlib.backend_bases.LocationEvent ` - mouse enters a new axes
68+ 'axes_leave_event' :class: `~matplotlib.backend_bases.LocationEvent ` - mouse leaves an axes
6569======================= ======================================================================================
6670
6771.. _event-attributes :
@@ -330,6 +334,66 @@ Extra credit solution::
330334 plt.show()
331335
332336
337+ .. _enter-leave-events :
338+
339+ Mouse enter and leave
340+ ======================
341+
342+ If you want to be notified when the mouse enters or leaves a figure or
343+ axes, you can connect to the figure/axes enter/leave events. Here is
344+ a simple example that changes the colors of the axes and figure
345+ background that the mouse is over::
346+
347+ """
348+ Illustrate the figure and axes enter and leave events by changing the
349+ frame colors on enter and leave
350+ """
351+ import matplotlib.pyplot as plt
352+
353+ def enter_axes(event):
354+ print 'enter_axes', event.inaxes
355+ event.inaxes.patch.set_facecolor('yellow')
356+ event.canvas.draw()
357+
358+ def leave_axes(event):
359+ print 'leave_axes', event.inaxes
360+ event.inaxes.patch.set_facecolor('white')
361+ event.canvas.draw()
362+
363+ def enter_figure(event):
364+ print 'enter_figure', event.canvas.figure
365+ event.canvas.figure.patch.set_facecolor('red')
366+ event.canvas.draw()
367+
368+ def leave_figure(event):
369+ print 'leave_figure', event.canvas.figure
370+ event.canvas.figure.patch.set_facecolor('grey')
371+ event.canvas.draw()
372+
373+ fig1 = plt.figure()
374+ fig1.suptitle('mouse hover over figure or axes to trigger events')
375+ ax1 = fig1.add_subplot(211)
376+ ax2 = fig1.add_subplot(212)
377+
378+ fig1.canvas.mpl_connect('figure_enter_event', enter_figure)
379+ fig1.canvas.mpl_connect('figure_leave_event', leave_figure)
380+ fig1.canvas.mpl_connect('axes_enter_event', enter_axes)
381+ fig1.canvas.mpl_connect('axes_leave_event', leave_axes)
382+
383+ fig2 = plt.figure()
384+ fig2.suptitle('mouse hover over figure or axes to trigger events')
385+ ax1 = fig2.add_subplot(211)
386+ ax2 = fig2.add_subplot(212)
387+
388+ fig2.canvas.mpl_connect('figure_enter_event', enter_figure)
389+ fig2.canvas.mpl_connect('figure_leave_event', leave_figure)
390+ fig2.canvas.mpl_connect('axes_enter_event', enter_axes)
391+ fig2.canvas.mpl_connect('axes_leave_event', leave_axes)
392+
393+ plt.show()
394+
395+
396+
333397.. _object-picking :
334398
335399Object picking
0 commit comments