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

Skip to content

Commit a8d1677

Browse files
daniilSQuLogic
andcommitted
Change cursor when hovering over draggable artists
Co-Authored-By: Elliott Sales de Andrade <[email protected]>
1 parent 5f650dd commit a8d1677

File tree

3 files changed

+45
-4
lines changed

3 files changed

+45
-4
lines changed

lib/matplotlib/axes/_base.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1298,6 +1298,9 @@ def __clear(self):
12981298
self.child_axes = []
12991299
self._current_image = None # strictly for pyplot via _sci, _gci
13001300
self._projection_init = None # strictly for pyplot.subplot
1301+
1302+
if hasattr(self, "legend_") and self.legend_ is not None:
1303+
self.legend_.axes = self.legend_.figure = None
13011304
self.legend_ = None
13021305
self.containers = []
13031306

lib/matplotlib/offsetbox.py

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import matplotlib.path as mpath
3333
import matplotlib.text as mtext
3434
import matplotlib.transforms as mtransforms
35+
from matplotlib.backend_tools import Cursors
3536
from matplotlib.font_manager import FontProperties
3637
from matplotlib.image import BboxImage
3738
from matplotlib.patches import (
@@ -1489,6 +1490,7 @@ def __init__(self, ref_artist, use_blit=False):
14891490
if not ref_artist.pickable():
14901491
ref_artist.set_picker(True)
14911492
self.got_artist = False
1493+
self._hover = False
14921494
self._use_blit = use_blit and self.canvas.supports_blit
14931495
callbacks = ref_artist.figure._canvas_callbacks
14941496
self._disconnectors = [
@@ -1508,7 +1510,33 @@ def __init__(self, ref_artist, use_blit=False):
15081510
disconnect.args[0] for disconnect in self._disconnectors[:2]])
15091511

15101512
def on_motion(self, evt):
1511-
if self._check_still_parented() and self.got_artist:
1513+
# Only check if the widget lock is available, setting it would prevent
1514+
# picking.
1515+
if not (
1516+
self._check_still_parented()
1517+
and self.canvas.widgetlock.available(self)
1518+
and self.ref_artist.pickable()
1519+
):
1520+
return
1521+
1522+
picker = self.ref_artist.get_picker()
1523+
if callable(picker):
1524+
inside, _ = picker(self, evt)
1525+
else:
1526+
inside, _ = self.ref_artist.contains(evt)
1527+
1528+
# If the mouse is moving quickly while dragging, it may leave the artist,
1529+
# but should still use the move cursor.
1530+
if inside or self.got_artist:
1531+
self._hover = True
1532+
self.canvas.set_cursor(Cursors.MOVE)
1533+
elif self._hover:
1534+
# Only change the cursor back if this is the widget that set it, to
1535+
# avoid multiple draggable widgets fighting over the cursor.
1536+
self._hover = False
1537+
self.canvas.set_cursor(Cursors.POINTER)
1538+
1539+
if self.got_artist:
15121540
dx = evt.x - self.mouse_x
15131541
dy = evt.y - self.mouse_y
15141542
self.update_offset(dx, dy)
@@ -1554,6 +1582,10 @@ def disconnect(self):
15541582
for disconnector in self._disconnectors:
15551583
disconnector()
15561584

1585+
if self._hover:
1586+
self._hover = False
1587+
self.canvas.set_cursor(Cursors.POINTER)
1588+
15571589
def save_offset(self):
15581590
pass
15591591

lib/matplotlib/tests/test_axes.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8644,13 +8644,19 @@ def test_extent_units():
86448644

86458645
def test_cla_clears_children_axes_and_fig():
86468646
fig, ax = plt.subplots()
8647-
lines = ax.plot([], [], [], [])
8647+
8648+
lines = ax.plot([], [], [], [], label="line")
86488649
img = ax.imshow([[1]])
8649-
for art in lines + [img]:
8650+
legend = ax.legend()
8651+
8652+
children = [*lines, img, legend]
8653+
8654+
for art in children:
86508655
assert art.axes is ax
86518656
assert art.figure is fig
8657+
86528658
ax.clear()
8653-
for art in lines + [img]:
8659+
for art in children:
86548660
assert art.axes is None
86558661
assert art.figure is None
86568662

0 commit comments

Comments
 (0)