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

Skip to content

Commit 699edf7

Browse files
committed
Create test for MultiCursor
1 parent 4ca6664 commit 699edf7

File tree

1 file changed

+41
-1
lines changed

1 file changed

+41
-1
lines changed

lib/matplotlib/tests/test_widgets.py

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import matplotlib.widgets as widgets
33
import matplotlib.pyplot as plt
44
from matplotlib.testing.decorators import image_comparison
5-
from matplotlib.testing.widgets import do_event, get_ax
5+
from matplotlib.testing.widgets import do_event, get_ax, mock_event
66

77
from numpy.testing import assert_allclose
88

@@ -460,3 +460,43 @@ def test_polygon_selector():
460460
+ polygon_place_vertex(50, 150)
461461
+ polygon_place_vertex(50, 50))
462462
check_polygon_selector(event_sequence, expected_result, 1)
463+
464+
465+
@pytest.mark.parametrize(
466+
"horizOn, vertOn",
467+
[(True, True), (True, False), (False, True)],
468+
)
469+
def test_MultiCursor(horizOn, vertOn):
470+
fig, (ax1, ax2, ax3) = plt.subplots(3, sharex=True)
471+
472+
# useblit=false to avoid having to draw the figure to cache the renderer
473+
multi = widgets.MultiCursor(
474+
fig.canvas, (ax1, ax2), useblit=False, horizOn=horizOn, vertOn=vertOn
475+
)
476+
477+
# Only two of the axes should have a line drawn on them.
478+
if vertOn:
479+
assert len(multi.vlines) == 2
480+
if horizOn:
481+
assert len(multi.hlines) == 2
482+
483+
# mock a motion_notify_event
484+
# Can't use `do_event` as that helper requires the widget
485+
# to have a single .ax attribute.
486+
event = mock_event(ax1, xdata=.5, ydata=.25)
487+
multi.onmove(event)
488+
489+
# the lines in the first two ax should both move
490+
for l in multi.vlines:
491+
assert l.get_xdata() == (.5, .5)
492+
for l in multi.hlines:
493+
assert l.get_ydata() == (.25, .25)
494+
495+
# test a move event in an axes not part of the MultiCursor
496+
# the lines in ax1 and ax2 should not have moved.
497+
event = mock_event(ax3, xdata=.75, ydata=.75)
498+
multi.onmove(event)
499+
for l in multi.vlines:
500+
assert l.get_xdata() == (.5, .5)
501+
for l in multi.hlines:
502+
assert l.get_ydata() == (.25, .25)

0 commit comments

Comments
 (0)