|
| 1 | +""" |
| 2 | +================ |
| 3 | +Hover event demo |
| 4 | +================ |
| 5 | +
|
| 6 | +.. note:: |
| 7 | + Data tooltips are currently only supported for the TkAgg backend. |
| 8 | +
|
| 9 | +You can enable hovering by setting the "hover" property of an artist. |
| 10 | +Hovering adds a tooltip to the bottom right corner |
| 11 | +of the figure canvas, which is displayed when the mouse pointer hovers over the |
| 12 | +artist. |
| 13 | +
|
| 14 | +The hover behavior depends on the type of the argument passed to the |
| 15 | +``set_hover`` method: |
| 16 | +
|
| 17 | +* *None* - hovering is disabled for this artist (default) |
| 18 | +
|
| 19 | +* list of string literals - hovering is enabled, and hovering over a point |
| 20 | + displays the corresponding string literal. |
| 21 | +
|
| 22 | +* function - if hover is callable, it is a user supplied function which |
| 23 | + takes a ``mouseevent`` object (see below), and returns a tuple of transformed |
| 24 | + coordinates |
| 25 | +
|
| 26 | +After you have enabled an artist for picking by setting the "hover" |
| 27 | +property, you need to connect to the figure canvas hover_event to get |
| 28 | +hover callbacks on mouse over events. For example, :: |
| 29 | +
|
| 30 | + def hover_handler(event): |
| 31 | + mouseevent = event.mouseevent |
| 32 | + artist = event.artist |
| 33 | + # now do something with this... |
| 34 | +
|
| 35 | +
|
| 36 | +The hover event (matplotlib.backend_bases.HoverEvent) which is passed to |
| 37 | +your callback is always fired with two attributes: |
| 38 | +
|
| 39 | +mouseevent |
| 40 | + the mouse event that generate the hover event. |
| 41 | +
|
| 42 | + The mouse event in turn has attributes like x and y (the coordinates in |
| 43 | + display space, such as pixels from left, bottom) and xdata, ydata (the |
| 44 | + coords in data space). Additionally, you can get information about |
| 45 | + which buttons were pressed, which keys were pressed, which Axes |
| 46 | + the mouse is over, etc. See matplotlib.backend_bases.MouseEvent |
| 47 | + for details. |
| 48 | +
|
| 49 | +artist |
| 50 | + the matplotlib.artist that generated the hover event. |
| 51 | +
|
| 52 | +You can set the ``hover`` property of an artist by supplying a ``hover`` |
| 53 | +argument to ``Axes.plot()`` |
| 54 | +
|
| 55 | +The examples below illustrate the different ways to use the ``hover`` property. |
| 56 | +
|
| 57 | +.. note:: |
| 58 | + These examples exercises the interactive capabilities of Matplotlib, and |
| 59 | + this will not appear in the static documentation. Please run this code on |
| 60 | + your machine to see the interactivity. |
| 61 | +
|
| 62 | + You can copy and paste individual parts, or download the entire example |
| 63 | + using the link at the bottom of the page. |
| 64 | +""" |
| 65 | +# %% |
| 66 | +# Hover with string literal labels |
| 67 | +# -------------------------------- |
| 68 | +import matplotlib.pyplot as plt |
| 69 | +from numpy.random import rand |
| 70 | + |
| 71 | +fig, ax = plt.subplots() |
| 72 | +plt.ylabel('some numbers') |
| 73 | + |
| 74 | +ax.plot(rand(3), 'o', hover=['London', 'Paris', 'Barcelona']) |
| 75 | +plt.show() |
| 76 | + |
| 77 | +# %% |
| 78 | +# Hover with a callable transformation function |
| 79 | +# --------------------------------------------- |
| 80 | +fig, ax = plt.subplots() |
| 81 | +plt.ylabel('some numbers') |
| 82 | + |
| 83 | + |
| 84 | +def user_defined_function(event): |
| 85 | + return round(event.xdata * 10, 1), round(event.ydata + 3, 3) |
| 86 | + |
| 87 | +ax.plot(rand(100), 'o', hover=user_defined_function) |
| 88 | +plt.show() |
0 commit comments