11"""
2- ===============
3- Pick event demo
4- ===============
2+ ================
3+ Hover event demo
4+ ================
55
6- You can enable picking by setting the "picker" property of an artist
7- (for example, a Matplotlib Line2D, Text, Patch, Polygon, AxesImage,
8- etc.)
9-
10- There are a variety of meanings of the picker property:
11-
12- * *None* - picking is disabled for this artist (default)
6+ .. note::
7+ Data tooltips are currently only supported for the TkAgg backend.
138
14- * bool - if *True* then picking will be enabled and the artist will fire a pick
15- event if the mouse event is over the artist.
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.
1613
17- Setting ``pickradius`` will add an epsilon tolerance in points and the artist
18- will fire off an event if its data is within epsilon of the mouse event. For
19- some artists like lines and patch collections, the artist may provide
20- additional data to the pick event that is generated, for example, the indices
21- of the data within epsilon of the pick event
14+ The hover behavior depends on the type of the argument passed to the
15+ ``set_hover`` method:
2216
23- * function - if picker is callable, it is a user supplied function which
24- determines whether the artist is hit by the mouse event. ::
17+ * *None* - hovering is disabled for this artist (default)
2518
26- hit, props = picker(artist, mouseevent)
19+ * list of string literals - hovering is enabled, and hovering over a point
20+ displays the corresponding string literal.
2721
28- to determine the hit test. If the mouse event is over the artist, return
29- hit=True and props is a dictionary of properties you want added to the
30- PickEvent attributes.
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
3125
32- After you have enabled an artist for picking by setting the "picker "
33- property, you need to connect to the figure canvas pick_event to get
34- pick callbacks on mouse press events. For example, ::
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, ::
3529
36- def pick_handler (event):
30+ def hover_handler (event):
3731 mouseevent = event.mouseevent
3832 artist = event.artist
3933 # now do something with this...
4034
4135
42- The pick event (matplotlib.backend_bases.PickEvent ) which is passed to
36+ The hover event (matplotlib.backend_bases.HoverEvent ) which is passed to
4337your callback is always fired with two attributes:
4438
4539mouseevent
46- the mouse event that generate the pick event.
40+ the mouse event that generate the hover event.
4741
4842 The mouse event in turn has attributes like x and y (the coordinates in
4943 display space, such as pixels from left, bottom) and xdata, ydata (the
@@ -53,14 +47,12 @@ def pick_handler(event):
5347 for details.
5448
5549artist
56- the matplotlib.artist that generated the pick event.
50+ the matplotlib.artist that generated the hover event.
5751
58- Additionally, certain artists like Line2D and PatchCollection may
59- attach additional metadata like the indices into the data that meet
60- the picker criteria (for example, all the points in the line that are within
61- the specified epsilon tolerance)
52+ You can set the ``hover`` property of an artist by supplying a ``hover``
53+ argument to ``Axes.plot()``
6254
63- The examples below illustrate each of these methods .
55+ The examples below illustrate the different ways to use the ``hover`` property .
6456
6557.. note::
6658 These examples exercises the interactive capabilities of Matplotlib, and
@@ -70,139 +62,27 @@ def pick_handler(event):
7062 You can copy and paste individual parts, or download the entire example
7163 using the link at the bottom of the page.
7264"""
73-
65+ # %%
66+ # Hover with string literal labels
67+ # --------------------------------
7468import matplotlib .pyplot as plt
75- import numpy as np
7669from numpy .random import rand
7770
78- from matplotlib .image import AxesImage
79- from matplotlib .lines import Line2D
80- from matplotlib .patches import Rectangle
81- from matplotlib .text import Text
82-
83- # Fixing random state for reproducibility
84- np .random .seed (19680801 )
85-
86-
87- # %%
88- # Simple picking, lines, rectangles and text
89- # ------------------------------------------
90-
91- fig , (ax1 , ax2 ) = plt .subplots (2 , 1 )
92- ax1 .set_title ('click on points, rectangles or text' , picker = True )
93- ax1 .set_ylabel ('ylabel' , picker = True , bbox = dict (facecolor = 'red' ))
94- line , = ax1 .plot (rand (100 ), 'o' , picker = True , pickradius = 5 )
95-
96- # Pick the rectangle.
97- ax2 .bar (range (10 ), rand (10 ), picker = True )
98- for label in ax2 .get_xticklabels (): # Make the xtick labels pickable.
99- label .set_picker (True )
100-
101-
102- def onpick1 (event ):
103- if isinstance (event .artist , Line2D ):
104- thisline = event .artist
105- xdata = thisline .get_xdata ()
106- ydata = thisline .get_ydata ()
107- ind = event .ind
108- print ('onpick1 line:' , np .column_stack ([xdata [ind ], ydata [ind ]]))
109- elif isinstance (event .artist , Rectangle ):
110- patch = event .artist
111- print ('onpick1 patch:' , patch .get_path ())
112- elif isinstance (event .artist , Text ):
113- text = event .artist
114- print ('onpick1 text:' , text .get_text ())
115-
116-
117- fig .canvas .mpl_connect ('pick_event' , onpick1 )
118-
119-
120- # %%
121- # Picking with a custom hit test function
122- # ---------------------------------------
123- # You can define custom pickers by setting picker to a callable function. The
124- # function has the signature::
125- #
126- # hit, props = func(artist, mouseevent)
127- #
128- # to determine the hit test. If the mouse event is over the artist, return
129- # ``hit=True`` and ``props`` is a dictionary of properties you want added to
130- # the `.PickEvent` attributes.
131-
132- def line_picker (line , mouseevent ):
133- """
134- Find the points within a certain distance from the mouseclick in
135- data coords and attach some extra attributes, pickx and picky
136- which are the data points that were picked.
137- """
138- if mouseevent .xdata is None :
139- return False , dict ()
140- xdata = line .get_xdata ()
141- ydata = line .get_ydata ()
142- maxd = 0.05
143- d = np .sqrt (
144- (xdata - mouseevent .xdata )** 2 + (ydata - mouseevent .ydata )** 2 )
145-
146- ind , = np .nonzero (d <= maxd )
147- if len (ind ):
148- pickx = xdata [ind ]
149- picky = ydata [ind ]
150- props = dict (ind = ind , pickx = pickx , picky = picky )
151- return True , props
152- else :
153- return False , dict ()
154-
155-
156- def onpick2 (event ):
157- print ('onpick2 line:' , event .pickx , event .picky )
158-
159-
160- fig , ax = plt .subplots ()
161- ax .set_title ('custom picker for line data' )
162- line , = ax .plot (rand (100 ), rand (100 ), 'o' , picker = line_picker )
163- fig .canvas .mpl_connect ('pick_event' , onpick2 )
164-
165-
166- # %%
167- # Picking on a scatter plot
168- # -------------------------
169- # A scatter plot is backed by a `~matplotlib.collections.PathCollection`.
170-
171- x , y , c , s = rand (4 , 100 )
172-
173-
174- def onpick3 (event ):
175- ind = event .ind
176- print ('onpick3 scatter:' , ind , x [ind ], y [ind ])
177-
178-
17971fig , ax = plt .subplots ()
180- ax .scatter (x , y , 100 * s , c , picker = True )
181- fig .canvas .mpl_connect ('pick_event' , onpick3 )
72+ plt .ylabel ('some numbers' )
18273
74+ ax .plot (rand (3 ), 'o' , hover = ['London' , 'Paris' , 'Barcelona' ])
75+ plt .show ()
18376
18477# %%
185- # Picking images
186- # --------------
187- # Images plotted using `.Axes.imshow` are `~matplotlib.image.AxesImage`
188- # objects.
189-
78+ # Hover with a callable transformation function
79+ # ---------------------------------------------
19080fig , ax = plt .subplots ()
191- ax .imshow (rand (10 , 5 ), extent = (1 , 2 , 1 , 2 ), picker = True )
192- ax .imshow (rand (5 , 10 ), extent = (3 , 4 , 1 , 2 ), picker = True )
193- ax .imshow (rand (20 , 25 ), extent = (1 , 2 , 3 , 4 ), picker = True )
194- ax .imshow (rand (30 , 12 ), extent = (3 , 4 , 3 , 4 ), picker = True )
195- ax .set (xlim = (0 , 5 ), ylim = (0 , 5 ))
196-
197-
198- def onpick4 (event ):
199- artist = event .artist
200- if isinstance (artist , AxesImage ):
201- im = artist
202- A = im .get_array ()
203- print ('onpick4 image' , A .shape )
81+ plt .ylabel ('some numbers' )
20482
20583
206- fig .canvas .mpl_connect ('pick_event' , onpick4 )
84+ def user_defined_function (event ):
85+ return round (event .xdata * 10 , 1 ), round (event .ydata + 3 , 3 )
20786
87+ ax .plot (rand (100 ), 'o' , hover = user_defined_function )
20888plt .show ()
0 commit comments