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

Skip to content

Commit 0a062ed

Browse files
authored
Merge pull request #3 from eslothower/yc-task2-tests
Test addition for hover
2 parents 844fcc8 + 2f87853 commit 0a062ed

File tree

4 files changed

+35
-3
lines changed

4 files changed

+35
-3
lines changed

lib/matplotlib/artist.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -649,6 +649,8 @@ def set_hover(self, hover):
649649
artist will fire a hover event if the mouse event is hovering over
650650
the artist.
651651
"""
652+
if not self.figure and hover is not None:
653+
raise ValueError("Cannot hover without an existing figure")
652654
self._hover = hover
653655

654656
def get_hover(self):

lib/matplotlib/backend_bases.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3016,8 +3016,9 @@ def mouse_move(self, event):
30163016
self.set_message(self._mouse_event_to_message(event))
30173017

30183018
if callable(getattr(self, 'set_hover_message', None)):
3019-
for a in self.canvas.figure.findobj(match=lambda x: not isinstance(x,
3020-
Rectangle), include_self=False):
3019+
def nonrect(x):
3020+
return not isinstance(x, Rectangle)
3021+
for a in self.canvas.figure.findobj(match=nonrect, include_self=False):
30213022
inside, prop = a.contains(event)
30223023
if inside:
30233024
self.set_hover_message(self._mouse_event_to_message(event))

lib/matplotlib/backends/backend_qt.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from matplotlib.backend_bases import (
1010
_Backend, FigureCanvasBase, FigureManagerBase, NavigationToolbar2,
1111
TimerBase, cursors, ToolContainerBase, MouseButton,
12-
CloseEvent, KeyEvent, LocationEvent, MouseEvent, ResizeEvent)
12+
CloseEvent, KeyEvent, LocationEvent, MouseEvent, ResizeEvent, HoverEvent)
1313
import matplotlib.backends.qt_editor.figureoptions as figureoptions
1414
from . import qt_compat
1515
from .qt_compat import (
@@ -305,6 +305,17 @@ def mouseReleaseEvent(self, event):
305305
modifiers=self._mpl_modifiers(),
306306
guiEvent=event)._process()
307307

308+
def mouseOverEvent(self, event):
309+
artist = event.artist
310+
if not artist.get_hover:
311+
thismouse = MouseEvent("motion_hover_event", self,
312+
*self.mouseEventCoords(event),
313+
modifiers=self._mpl_modifiers(),
314+
guiEvent=event)
315+
hovering = HoverEvent("motion_hover_event", self,
316+
thismouse, artist, None)
317+
hovering._process()
318+
308319
def wheelEvent(self, event):
309320
# from QWheelEvent::pixelDelta doc: pixelDelta is sometimes not
310321
# provided (`isNull()`) and is unreliable on X11 ("xcb").

lib/matplotlib/tests/test_artist.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,24 @@ def test_set_alpha_for_array():
325325
art._set_alpha_for_array([0.5, np.nan])
326326

327327

328+
def test_set_hover():
329+
art = martist.Artist() # blank canvas
330+
with pytest.raises(ValueError, match="Cannot hover without an existing figure"):
331+
art.set_hover(True)
332+
333+
fig, ax = plt.subplots()
334+
im = ax.imshow(np.arange(36).reshape(6, 6)) # non-blank canvas
335+
336+
im.set_hover(True) # set hover variable to possible values given a figure exists
337+
assert im.get_hover()
338+
im.set_hover(False)
339+
assert not im.get_hover()
340+
im.set_hover(None)
341+
assert im.get_hover() is None
342+
343+
im.remove()
344+
345+
328346
def test_callbacks():
329347
def func(artist):
330348
func.counter += 1

0 commit comments

Comments
 (0)