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

Skip to content

Commit aad38a0

Browse files
authored
Merge pull request #23197 from oscargus/pantests
Add tests for pan
2 parents 30b320a + fcefcc6 commit aad38a0

File tree

1 file changed

+56
-1
lines changed

1 file changed

+56
-1
lines changed

lib/matplotlib/tests/test_backend_bases.py

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,8 @@ def test_interactive_zoom():
180180
@pytest.mark.parametrize("tool,button,expected",
181181
[("zoom", MouseButton.LEFT, (4, 6)), # zoom in
182182
("zoom", MouseButton.RIGHT, (-20, 30)), # zoom out
183-
("pan", MouseButton.LEFT, (-2, 8))])
183+
("pan", MouseButton.LEFT, (-2, 8)),
184+
("pan", MouseButton.RIGHT, (1.47, 7.78))]) # zoom
184185
def test_interactive_colorbar(plot_func, orientation, tool, button, expected):
185186
fig, ax = plt.subplots()
186187
data = np.arange(12).reshape((4, 3))
@@ -284,3 +285,57 @@ def test_draw(backend):
284285

285286
for ref, test in zip(layed_out_pos_agg, layed_out_pos_test):
286287
np.testing.assert_allclose(ref, test, atol=0.005)
288+
289+
290+
@pytest.mark.parametrize(
291+
"key,mouseend,expectedxlim,expectedylim",
292+
[(None, (0.2, 0.2), (3.49, 12.49), (2.7, 11.7)),
293+
(None, (0.2, 0.5), (3.49, 12.49), (0, 9)),
294+
(None, (0.5, 0.2), (0, 9), (2.7, 11.7)),
295+
(None, (0.5, 0.5), (0, 9), (0, 9)), # No move
296+
(None, (0.8, 0.25), (-3.47, 5.53), (2.25, 11.25)),
297+
(None, (0.2, 0.25), (3.49, 12.49), (2.25, 11.25)),
298+
(None, (0.8, 0.85), (-3.47, 5.53), (-3.14, 5.86)),
299+
(None, (0.2, 0.85), (3.49, 12.49), (-3.14, 5.86)),
300+
("shift", (0.2, 0.4), (3.49, 12.49), (0, 9)), # snap to x
301+
("shift", (0.4, 0.2), (0, 9), (2.7, 11.7)), # snap to y
302+
("shift", (0.2, 0.25), (3.49, 12.49), (3.49, 12.49)), # snap to diagonal
303+
("shift", (0.8, 0.25), (-3.47, 5.53), (3.47, 12.47)), # snap to diagonal
304+
("shift", (0.8, 0.9), (-3.58, 5.41), (-3.58, 5.41)), # snap to diagonal
305+
("shift", (0.2, 0.85), (3.49, 12.49), (-3.49, 5.51)), # snap to diagonal
306+
("x", (0.2, 0.1), (3.49, 12.49), (0, 9)), # only x
307+
("y", (0.1, 0.2), (0, 9), (2.7, 11.7)), # only y
308+
("control", (0.2, 0.2), (3.49, 12.49), (3.49, 12.49)), # diagonal
309+
("control", (0.4, 0.2), (2.72, 11.72), (2.72, 11.72)), # diagonal
310+
])
311+
def test_interactive_pan(key, mouseend, expectedxlim, expectedylim):
312+
fig, ax = plt.subplots()
313+
ax.plot(np.arange(10))
314+
assert ax.get_navigate()
315+
# Set equal aspect ratio to easier see diagonal snap
316+
ax.set_aspect('equal')
317+
318+
# Mouse move starts from 0.5, 0.5
319+
mousestart = (0.5, 0.5)
320+
# Convert to screen coordinates ("s"). Events are defined only with pixel
321+
# precision, so round the pixel values, and below, check against the
322+
# corresponding xdata/ydata, which are close but not equal to d0/d1.
323+
sstart = ax.transData.transform(mousestart).astype(int)
324+
send = ax.transData.transform(mouseend).astype(int)
325+
326+
# Set up the mouse movements
327+
start_event = MouseEvent(
328+
"button_press_event", fig.canvas, *sstart, button=MouseButton.LEFT,
329+
key=key)
330+
stop_event = MouseEvent(
331+
"button_release_event", fig.canvas, *send, button=MouseButton.LEFT,
332+
key=key)
333+
334+
tb = NavigationToolbar2(fig.canvas)
335+
tb.pan()
336+
tb.press_pan(start_event)
337+
tb.drag_pan(stop_event)
338+
tb.release_pan(stop_event)
339+
# Should be close, but won't be exact due to screen integer resolution
340+
assert tuple(ax.get_xlim()) == pytest.approx(expectedxlim, abs=0.02)
341+
assert tuple(ax.get_ylim()) == pytest.approx(expectedylim, abs=0.02)

0 commit comments

Comments
 (0)