Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 41 additions & 1 deletion lib/matplotlib/tests/test_widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import matplotlib.widgets as widgets
import matplotlib.pyplot as plt
from matplotlib.testing.decorators import image_comparison
from matplotlib.testing.widgets import do_event, get_ax
from matplotlib.testing.widgets import do_event, get_ax, mock_event

from numpy.testing import assert_allclose

Expand Down Expand Up @@ -460,3 +460,43 @@ def test_polygon_selector():
+ polygon_place_vertex(50, 150)
+ polygon_place_vertex(50, 50))
check_polygon_selector(event_sequence, expected_result, 1)


@pytest.mark.parametrize(
"horizOn, vertOn",
[(True, True), (True, False), (False, True)],
)
def test_MultiCursor(horizOn, vertOn):
fig, (ax1, ax2, ax3) = plt.subplots(3, sharex=True)

# useblit=false to avoid having to draw the figure to cache the renderer
multi = widgets.MultiCursor(
fig.canvas, (ax1, ax2), useblit=False, horizOn=horizOn, vertOn=vertOn
)

# Only two of the axes should have a line drawn on them.
if vertOn:
assert len(multi.vlines) == 2
if horizOn:
assert len(multi.hlines) == 2

# mock a motion_notify_event
# Can't use `do_event` as that helper requires the widget
# to have a single .ax attribute.
event = mock_event(ax1, xdata=.5, ydata=.25)
multi.onmove(event)

# the lines in the first two ax should both move
for l in multi.vlines:
assert l.get_xdata() == (.5, .5)
for l in multi.hlines:
assert l.get_ydata() == (.25, .25)

# test a move event in an axes not part of the MultiCursor
# the lines in ax1 and ax2 should not have moved.
event = mock_event(ax3, xdata=.75, ydata=.75)
multi.onmove(event)
for l in multi.vlines:
assert l.get_xdata() == (.5, .5)
for l in multi.hlines:
assert l.get_ydata() == (.25, .25)
2 changes: 1 addition & 1 deletion lib/matplotlib/widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -1653,7 +1653,7 @@ def clear(self, event):
def onmove(self, event):
if self.ignore(event):
return
if event.inaxes is None:
if event.inaxes not in self.axes:
return
if not self.canvas.widgetlock.available(self):
return
Expand Down