Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e2b2c0e

Browse files
authored
Merge pull request #1 from eslothower/Eli---Task-1
Add tooltip API to backend_bases.py and get/set_hover() to artist.pyi
2 parents 2c4f552 + b6c5d75 commit e2b2c0e

File tree

8 files changed

+120
-8
lines changed

8 files changed

+120
-8
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,3 +110,5 @@ lib/matplotlib/backends/web_backend/node_modules/
110110
lib/matplotlib/backends/web_backend/package-lock.json
111111

112112
LICENSE/LICENSE_QHULL
113+
bin/
114+
lib/python3.8/site-packages/

doc/api/next_api_changes/development/00001-ABC.rst

Lines changed: 0 additions & 7 deletions
This file was deleted.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
- The signature and implementation of the `set_hover()` and `get_hover()` methods of the `Artist` class, as well as the `HoverEvent(Event)` Class, have been added:
2+
3+
.. versionchanged:: 3.7.2
4+
The `set_hover()` method takes two arguments, `self` and `hover`. `hover` is the hover status that the object is then set to.
5+
The `get_hover()` methods take a single argument `self` and returns the hover status of the object.
6+
The `HoverEvent(Event)` Class takes a single argument `Event` and fires when the mouse hovers over a canvas.

lib/matplotlib/artist.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,7 @@ def __init__(self):
193193
self._clipon = True
194194
self._label = ''
195195
self._picker = None
196+
self._hover = None
196197
self._rasterized = False
197198
self._agg_filter = None
198199
# Normally, artist classes need to be queried for mouseover info if and
@@ -594,6 +595,49 @@ def get_picker(self):
594595
set_picker, pickable, pick
595596
"""
596597
return self._picker
598+
599+
def set_hover(self, hover):
600+
"""
601+
Define the hover status of the artist.
602+
603+
Parameters
604+
----------
605+
hover : None or bool or float or callable
606+
This can be one of the following:
607+
608+
- *None*: Hover is disabled for this artist (default).
609+
610+
- A boolean: If *True* then hover will be enabled and the
611+
artist will fire a hover event if the mouse event is hovering over
612+
the artist.
613+
614+
- A float: If hover is a number it is interpreted as an
615+
epsilon tolerance in points and the artist will fire
616+
off an event if its data is within epsilon of the mouse
617+
event. For some artists like lines and patch collections,
618+
the artist may provide additional data to the hover event
619+
that is generated, e.g., the indices of the data within
620+
epsilon of the hover event
621+
622+
- A function: If hover is callable, it is a user supplied
623+
function which determines whether the artist is hit by the
624+
mouse event to determine the hit test. if the mouse event
625+
is over the artist, return *hit=True* and props is a dictionary of
626+
properties you want added to the HoverEvent attributes.
627+
"""
628+
self._hover = hover
629+
630+
def get_hover(self):
631+
"""
632+
Return the hover status of the artist.
633+
634+
The possible values are described in `.set_hover`.
635+
636+
See Also
637+
--------
638+
set_hover
639+
"""
640+
return self._hover
597641

598642
def get_url(self):
599643
"""Return the url."""

lib/matplotlib/artist.pyi

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,18 @@ class Artist:
7676
) -> None | bool | float | Callable[
7777
[Artist, MouseEvent], tuple[bool, dict[Any, Any]]
7878
]: ...
79+
def set_hover(
80+
self,
81+
hover: None
82+
| bool
83+
| float
84+
| Callable[[Artist, MouseEvent], str],
85+
) -> None: ...
86+
def get_hover(
87+
self,
88+
) -> None | bool | float | Callable[
89+
[Artist, MouseEvent], str
90+
]: ...
7991
def get_url(self) -> str | None: ...
8092
def set_url(self, url: str | None) -> None: ...
8193
def get_gid(self) -> str | None: ...

lib/matplotlib/backend_bases.py

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1473,7 +1473,54 @@ def __str__(self):
14731473
f"xy=({self.x}, {self.y}) xydata=({self.xdata}, {self.ydata}) "
14741474
f"button={self.button} dblclick={self.dblclick} "
14751475
f"inaxes={self.inaxes}")
1476+
1477+
class HoverEvent(Event):
1478+
"""
1479+
A hover event.
1480+
1481+
This event is fired when the mouse is moved on the canvas
1482+
sufficiently close to an artist that has been made hoverable with
1483+
`.Artist.set_hover`.
1484+
1485+
A HoverEvent has a number of special attributes in addition to those defined
1486+
by the parent `Event` class.
1487+
1488+
Attributes
1489+
----------
1490+
mouseevent : `MouseEvent`
1491+
The mouse event that generated the hover.
1492+
artist : `matplotlib.artist.Artist`
1493+
The hovered artist. Note that artists are not hoverable by default
1494+
(see `.Artist.set_hover`).
1495+
other
1496+
Additional attributes may be present depending on the type of the
1497+
hovered object; e.g., a `.Line2D` hover may define different extra
1498+
attributes than a `.PatchCollection` hover.
1499+
1500+
Examples
1501+
--------
1502+
Bind a function ``on_hover()`` to hover events, that prints the coordinates
1503+
of the hovered data point::
1504+
1505+
ax.plot(np.rand(100), 'o', picker=5) # 5 points tolerance
1506+
1507+
def on_hover(event):
1508+
line = event.artist
1509+
xdata, ydata = line.get_data()
1510+
ind = event.ind
1511+
print(f'on hover line: {xdata[ind]:.3f}, {ydata[ind]:.3f}')
1512+
1513+
cid = fig.canvas.mpl_connect('motion_notify_event', on_hover)
1514+
"""
14761515

1516+
def __init__(self, name, canvas, mouseevent, artist,
1517+
guiEvent=None, **kwargs):
1518+
if guiEvent is None:
1519+
guiEvent = mouseevent.guiEvent
1520+
super().__init__(name, canvas, guiEvent)
1521+
self.mouseevent = mouseevent
1522+
self.artist = artist
1523+
self.__dict__.update(kwargs)
14771524

14781525
class PickEvent(Event):
14791526
"""
@@ -1524,7 +1571,7 @@ def __init__(self, name, canvas, mouseevent, artist,
15241571
self.__dict__.update(kwargs)
15251572

15261573

1527-
class KeyEvent(LocationEvent):
1574+
class KeyEvent(LocationEvent):
15281575
"""
15291576
A key event (key press, key release).
15301577

lib/matplotlib/backend_bases.pyi

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -495,3 +495,8 @@ class _Backend:
495495

496496
class ShowBase(_Backend):
497497
def __call__(self, block: bool | None = ...): ...
498+
499+
class HoverEvent:
500+
def __init__(self, name, canvas, mouseevent, artist,
501+
guiEvent=None, **kwargs):
502+
pass

pyvenv.cfg

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
home = /Users/eslothower/opt/anaconda3/bin
2+
include-system-site-packages = false
3+
version = 3.8.8

0 commit comments

Comments
 (0)