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

Skip to content

Commit 78d321f

Browse files
committed
Add tests for pan
1 parent 33e428d commit 78d321f

File tree

1 file changed

+58
-1
lines changed

1 file changed

+58
-1
lines changed

lib/matplotlib/tests/test_backend_bases.py

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,8 @@ def test_interactive_zoom():
183183
@pytest.mark.parametrize("tool,button,expected",
184184
[("zoom", MouseButton.LEFT, (4, 6)), # zoom in
185185
("zoom", MouseButton.RIGHT, (-20, 30)), # zoom out
186-
("pan", MouseButton.LEFT, (-2, 8))])
186+
("pan", MouseButton.LEFT, (-2, 8)),
187+
("pan", MouseButton.RIGHT, (1.47, 7.78))]) # zoom
187188
def test_interactive_colorbar(plot_func, orientation, tool, button, expected):
188189
fig, ax = plt.subplots()
189190
data = np.arange(12).reshape((4, 3))
@@ -286,3 +287,59 @@ def test_draw(backend):
286287

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

0 commit comments

Comments
 (0)