8000 Merge pull request #25129 from ksunden/undeprecate_cursor · matplotlib/matplotlib@ce21d72 · GitHub
[go: up one dir, main page]

Skip to content

Commit ce21d72

Browse files
authored
Merge pull request #25129 from ksunden/undeprecate_cursor
Undeprecate Cursor event handlers
2 parents bc2bf2b + 17542db commit ce21d72

File tree

4 files changed

+50
-37
lines changed

4 files changed

+50
-37
lines changed

examples/event_handling/cursor_demo.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
import matplotlib.pyplot as plt
2929
import numpy as np
3030

31+
from matplotlib.backend_bases import MouseEvent
32+
3133

3234
class Cursor:
3335
"""
@@ -71,6 +73,11 @@ def on_mouse_move(self, event):
7173
cursor = Cursor(ax)
7274
fig.canvas.mpl_connect('motion_notify_event', cursor.on_mouse_move)
7375

76+
# Simulate a mouse move to (0.5, 0.5), needed for online docs
77+
t = ax.transData
78+
MouseEvent(
79+
"motion_notify_event", ax.figure.canvas, *t.transform((0.5, 0.5))
80+
)._process()
7481

7582
# %%
7683
# Faster redrawing using blitting
@@ -85,6 +92,7 @@ def on_mouse_move(self, event):
8592
# created whenever the figure changes. This is achieved by connecting to the
8693
# ``'draw_event'``.
8794

95+
8896
class BlittedCursor:
8997
"""
9098
A cross-hair cursor using blitting for faster redraw.
@@ -152,6 +160,11 @@ def on_mouse_move(self, event):
152160
blitted_cursor = BlittedCursor(ax)
153161
fig.canvas.mpl_connect('motion_notify_event', blitted_cursor.on_mouse_move)
154162

163+
# Simulate a mouse move to (0.5, 0.5), needed for online docs
164+
t = ax.transData
165+
MouseEvent(
166+
"motion_notify_event", ax.figure.canvas, *t.transform((0.5, 0.5))
167+
)._process()
155168

156169
# %%
157170
# Snapping to data points
@@ -165,6 +178,7 @@ def on_mouse_move(self, event):
165178
# the lag due to many redraws. Of course, blitting could still be added on top
166179
# for additional speedup.
167180

181+
168182
class SnappingCursor:
169183
"""
170184
A cross-hair cursor that snaps to the data point of a line, which is
@@ -218,4 +232,11 @@ def on_mouse_move(self, event):
218232
line, = ax.plot(x, y, 'o')
219233
snap_cursor = SnappingCursor(ax, line)
220234
fig.canvas.mpl_connect('motion_notify_event', snap_cursor.on_mouse_move)
235+
236+
# Simulate a mouse move to (0.5, 0.5), needed for online docs
237+
t = ax.transData
238+
MouseEvent(
239+
"motion_notify_event", ax.figure.canvas, *t.transform((0.5, 0.5))
240+
)._process()
241+
221242
plt.show()

examples/widgets/annotated_cursor.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
import numpy as np
2525
import matplotlib.pyplot as plt
2626

27+
from matplotlib.backend_bases import MouseEvent
28+
2729

2830
class AnnotatedCursor(Cursor):
2931
"""
@@ -312,6 +314,12 @@ def _update(self):
312314
color='red',
313315
linewidth=2)
314316

317+
# Simulate a mouse move to (-2, 10), needed for online docs
318+
t = ax.transData
319+
MouseEvent(
320+
"motion_notify_event", ax.figure.canvas, *t.transform((-2, 10))
321+
)._process()
322+
315323
plt.show()
316324

317325
# %%
@@ -339,4 +347,10 @@ def _update(self):
339347
useblit=True,
340348
color='red', linewidth=2)
341349

350+
# Simulate a mouse move to (-2, 10), needed for online docs
351+
t = ax.transData
352+
MouseEvent(
353+
"motion_notify_event", ax.figure.canvas, *t.transform((-2, 10))
354+
)._process()
355+
342356
plt.show()

lib/matplotlib/tests/test_widgets.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1654,7 +1654,9 @@ def test_MultiCursor(horizOn, vertOn):
16541654
# Can't use `do_event` as that helper requires the widget
16551655
# to have a single .ax attribute.
16561656
event = mock_event(ax1, xdata=.5, ydata=.25)
1657-
multi._onmove(event)
1657+
multi.onmove(event)
1658+
# force a draw + draw event to exercise clear
1659+
ax1.figure.canvas.draw()
16581660

16591661
# the lines in the first two ax should both move
16601662
for l in multi.vlines:
@@ -1671,7 +1673,7 @@ def test_MultiCursor(horizOn, vertOn):
16711673
multi.horizOn = not multi.horizOn
16721674
multi.vertOn = not multi.vertOn
16731675
event = mock_event(ax1, xdata=.5, ydata=.25)
1674-
multi._onmove(event)
1676+
multi.onmove(event)
16751677
assert len([line for line in multi.vlines if line.get_visible()]) == (
16761678
0 if vertOn else 2)
16771679
assert len([line for line in multi.hlines if line.get_visible()]) == (
@@ -1680,7 +1682,7 @@ def test_MultiCursor(horizOn, vertOn):
16801682
# test a move event in an Axes not part of the MultiCursor
16811683
# the lines in ax1 and ax2 should not have moved.
16821684
event = mock_event(ax3, xdata=.75, ydata=.75)
1683-
multi._onmove(event)
1685+
multi.onmove(event)
16841686
for l in multi.vlines:
16851687
assert l.get_xdata() == (.5, .5)
16861688
for l in multi.hlines:

lib/matplotlib/widgets.py

Lines changed: 10 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1953,8 +1953,8 @@ def __init__(self, ax, horizOn=True, vertOn=True, useblit=False,
19531953
**lineprops):
19541954
super().__init__(ax)
19551955

1956-
self.connect_event('motion_notify_event', self._onmove)
1957-
self.connect_event('draw_event', self._clear)
1956+
self.connect_event('motion_notify_event', self.onmove)
1957+
self.connect_event('draw_event', self.clear)
19581958

19591959
self.visible = True
19601960
self.horizOn = horizOn
@@ -1967,29 +1967,16 @@ def __init__(self, ax, horizOn=True, vertOn=True, useblit=False,
19671967
self.linev = ax.axvline(ax.get_xbound()[0], visible=False, **lineprops)
19681968

19691969
self.background = None
1970-
self._needclear = False
1971-
1972-
needclear = _api.deprecate_privatize_attribute("3.7")
1970+
self.needclear = False
19731971

1974-
@_api.deprecated('3.7')
19751972
def clear(self, event):
1976-
"""Internal event handler to clear the cursor."""
1977-
self._clear(event)
1978-
if self.ignore(event):
1979-
return
1980-
self.linev.set_visible(False)
1981-
self.lineh.set_visible(False)
1982-
1983-
def _clear(self, event):
19841973
"""Internal event handler to clear the cursor."""
19851974
if self.ignore(event):
19861975
return
19871976
if self.useblit:
19881977
self.background = self.canvas.copy_from_bbox(self.ax.bbox)
19891978

1990-
onmove = _api.deprecate_privatize_attribute('3.7')
1991-
1992-
def _onmove(self, event):
1979+
def onmove(self, event):
19931980
"""Internal event handler to draw the cursor when the mouse moves."""
19941981
if self.ignore(event):
19951982
return
@@ -1999,11 +1986,11 @@ def _onmove(self, event):
19991986
self.linev.set_visible(False)
20001987
self.lineh.set_visible(False)
20011988

2002-
if self._needclear:
1989+
if self.needclear:
20031990
self.canvas.draw()
2004-
self._needclear = False
1991+
self.needclear = False
20051992
return
2006-
self._needclear = True
1993+
self.needclear = True
20071994

20081995
self.linev.set_xdata((event.xdata, event.xdata))
20091996
self.linev.set_visible(self.visible and self.vertOn)
@@ -2106,8 +2093,8 @@ def connect(self):
21062093
"""Connect events."""
21072094
for canvas, info in self._canvas_infos.items():
21082095
info["cids"] = [
2109-
canvas.mpl_connect('motion_notify_event', self._onmove),
2110-
canvas.mpl_connect('draw_event', self._clear),
2096+
canvas.mpl_connect('motion_notify_event', self.onmove),
2097+
canvas.mpl_connect('draw_event', self.clear),
21112098
]
21122099

21132100
def disconnect(self):
@@ -2117,26 +2104,15 @@ def disconnect(self):
21172104
canvas.mpl_disconnect(cid)
21182105
info["cids"].clear()
21192106

2120-
@_api.deprecated('3.7')
21212107
def clear(self, event):
2122-
"""Clear the cursor."""
2123-
if self.ignore(event):
2124-
return
2125-
self._clear(event)
2126-
for line in self.vlines + self.hlines:
2127-
line.set_visible(False)
2128-
2129-
def _clear(self, event):
21302108
"""Clear the cursor."""
21312109
if self.ignore(event):
21322110
return
21332111
if self.useblit:
21342112
for canvas, info in self._canvas_infos.items():
21352113
info["background"] = canvas.copy_from_bbox(canvas.figure.bbox)
21362114

2137-
onmove = _api.deprecate_privatize_attribute('3.7')
2138-
2139-
def _onmove(self, event):
2115+
def onmove(self, event):
21402116
if (self.ignore(event)
21412117
or event.inaxes not in self.axes
21422118
or not event.canvas.widgetlock.available(self)):

0 commit comments

Comments
 (0)
0