|
2 | 2 | import matplotlib.widgets as widgets
|
3 | 3 | import matplotlib.pyplot as plt
|
4 | 4 | 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 |
6 | 6 |
|
7 | 7 | from numpy.testing import assert_allclose
|
8 | 8 |
|
@@ -460,3 +460,43 @@ def test_polygon_selector():
|
460 | 460 | + polygon_place_vertex(50, 150)
|
461 | 461 | + polygon_place_vertex(50, 50))
|
462 | 462 | 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