Skip to content

Commit 2040a32

Browse files
committed
Fix toggling of MultiCursor.{horizOn,vertOn}
By implementing `_onmove` in a similar manner to `Cursor`. Also, deprecate the related `needclear` attribute in both widgets. Followup to #19763.
1 parent 65e54af commit 2040a32

File tree

3 files changed

+35
-28
lines changed

3 files changed

+35
-28
lines changed

doc/api/next_api_changes/deprecations/19763-ES.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
33

44
Access to the event handlers for the `.Cursor` and `.MultiCursor` widgets is
5-
now deprecated.
5+
now deprecated. The related *needclear* attribute is also deprecated.

lib/matplotlib/tests/test_widgets.py

+17-2
Original file line numberDiff line numberDiff line change
@@ -1575,8 +1575,8 @@ def test_MultiCursor(horizOn, vertOn):
15751575
)
15761576

15771577
# Only two of the axes should have a line drawn on them.
1578-
assert len(multi.vlines) == (2 if vertOn else 0)
1579-
assert len(multi.hlines) == (2 if horizOn else 0)
1578+
assert len(multi.vlines) == 2
1579+
assert len(multi.hlines) == 2
15801580

15811581
# mock a motion_notify_event
15821582
# Can't use `do_event` as that helper requires the widget
@@ -1589,6 +1589,21 @@ def test_MultiCursor(horizOn, vertOn):
15891589
assert l.get_xdata() == (.5, .5)
15901590
for l in multi.hlines:
15911591
assert l.get_ydata() == (.25, .25)
1592+
# The relevant lines get turned on after move.
1593+
assert len([line for line in multi.vlines if line.get_visible()]) == (
1594+
2 if vertOn else 0)
1595+
assert len([line for line in multi.hlines if line.get_visible()]) == (
1596+
2 if horizOn else 0)
1597+
1598+
# After toggling settings, the opposite lines should be visible after move.
1599+
multi.horizOn = not multi.horizOn
1600+
multi.vertOn = not multi.vertOn
1601+
event = mock_event(ax1, xdata=.5, ydata=.25)
1602+
multi._onmove(event)
1603+
assert len([line for line in multi.vlines if line.get_visible()]) == (
1604+
0 if vertOn else 2)
1605+
assert len([line for line in multi.hlines if line.get_visible()]) == (
1606+
0 if horizOn else 2)
15921607

15931608
# test a move event in an Axes not part of the MultiCursor
15941609
# the lines in ax1 and ax2 should not have moved.

lib/matplotlib/widgets.py

+17-25
Original file line numberDiff line numberDiff line change
@@ -1746,7 +1746,9 @@ def __init__(self, ax, horizOn=True, vertOn=True, useblit=False,
17461746
self.linev = ax.axvline(ax.get_xbound()[0], visible=False, **lineprops)
17471747

17481748
self.background = None
1749-
self.needclear = False
1749+
self._needclear = False
1750+
1751+
needclear = _api.deprecate_privatize_attribute("3.7")
17501752

17511753
@_api.deprecated('3.7')
17521754
def clear(self, event):
@@ -1776,11 +1778,11 @@ def _onmove(self, event):
17761778
self.linev.set_visible(False)
17771779
self.lineh.set_visible(False)
17781780

1779-
if self.needclear:
1781+
if self._needclear:
17801782
self.canvas.draw()
1781-
self.needclear = False
1783+
self._needclear = False
17821784
return
1783-
self.needclear = True
1785+
self._needclear = True
17841786

17851787
self.linev.set_xdata((event.xdata, event.xdata))
17861788
self.linev.set_visible(self.visible and self.vertOn)
@@ -1863,28 +1865,21 @@ def __init__(self, canvas, axes, useblit=True, horizOn=False, vertOn=True,
18631865
self.useblit = (
18641866
useblit
18651867
and all(canvas.supports_blit for canvas in self._canvas_infos))
1866-
self.needclear = False
18671868

18681869
if self.useblit:
18691870
lineprops['animated'] = True
18701871

1871-
if vertOn:
1872-
self.vlines = [ax.axvline(xmid, visible=False, **lineprops)
1873-
for ax in axes]
1874-
else:
1875-
self.vlines = []
1876-
1877-
if horizOn:
1878-
self.hlines = [ax.axhline(ymid, visible=False, **lineprops)
1879-
for ax in axes]
1880-
else:
1881-
self.hlines = []
1872+
self.vlines = [ax.axvline(xmid, visible=False, **lineprops)
1873+
for ax in axes]
1874+
self.hlines = [ax.axhline(ymid, visible=False, **lineprops)
1875+
for ax in axes]
18821876

18831877
self.connect()
18841878

18851879
canvas = _api.deprecate_privatize_attribute("3.6")
18861880
background = _api.deprecated("3.6")(lambda self: (
18871881
self._backgrounds[self.axes[0].figure.canvas] if self.axes else None))
1882+
needclear = _api.deprecated("3.7")(lambda self: False)
18881883

18891884
def connect(self):
18901885
"""Connect events."""
@@ -1925,15 +1920,12 @@ def _onmove(self, event):
19251920
or event.inaxes not in self.axes
19261921
or not event.canvas.widgetlock.available(self)):
19271922
return
1928-
self.needclear = True
1929-
if self.vertOn:
1930-
for line in self.vlines:
1931-
line.set_xdata((event.xdata, event.xdata))
1932-
line.set_visible(self.visible)
1933-
if self.horizOn:
1934-
for line in self.hlines:
1935-
line.set_ydata((event.ydata, event.ydata))
1936-
line.set_visible(self.visible)
1923+
for line in self.vlines:
1924+
line.set_xdata((event.xdata, event.xdata))
1925+
line.set_visible(self.visible and self.vertOn)
1926+
for line in self.hlines:
1927+
line.set_ydata((event.ydata, event.ydata))
1928+
line.set_visible(self.visible and self.horizOn)
19371929
if self.visible and (self.vertOn or self.horizOn):
19381930
self._update()
19391931

0 commit comments

Comments
 (0)