test_backend_bases.py 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586
  1. import importlib
  2. from matplotlib import path, transforms
  3. from matplotlib.backend_bases import (
  4. FigureCanvasBase, KeyEvent, LocationEvent, MouseButton, MouseEvent,
  5. NavigationToolbar2, RendererBase)
  6. from matplotlib.backend_tools import RubberbandBase
  7. from matplotlib.figure import Figure
  8. from matplotlib.testing._markers import needs_pgf_xelatex
  9. import matplotlib.pyplot as plt
  10. import numpy as np
  11. import pytest
  12. _EXPECTED_WARNING_TOOLMANAGER = (
  13. r"Treat the new Tool classes introduced in "
  14. r"v[0-9]*.[0-9]* as experimental for now; "
  15. "the API and rcParam may change in future versions.")
  16. def test_uses_per_path():
  17. id = transforms.Affine2D()
  18. paths = [path.Path.unit_regular_polygon(i) for i in range(3, 7)]
  19. tforms_matrices = [id.rotate(i).get_matrix().copy() for i in range(1, 5)]
  20. offsets = np.arange(20).reshape((10, 2))
  21. facecolors = ['red', 'green']
  22. edgecolors = ['red', 'green']
  23. def check(master_transform, paths, all_transforms,
  24. offsets, facecolors, edgecolors):
  25. rb = RendererBase()
  26. raw_paths = list(rb._iter_collection_raw_paths(
  27. master_transform, paths, all_transforms))
  28. gc = rb.new_gc()
  29. ids = [path_id for xo, yo, path_id, gc0, rgbFace in
  30. rb._iter_collection(
  31. gc, range(len(raw_paths)), offsets,
  32. transforms.AffineDeltaTransform(master_transform),
  33. facecolors, edgecolors, [], [], [False],
  34. [], 'screen')]
  35. uses = rb._iter_collection_uses_per_path(
  36. paths, all_transforms, offsets, facecolors, edgecolors)
  37. if raw_paths:
  38. seen = np.bincount(ids, minlength=len(raw_paths))
  39. assert set(seen).issubset([uses - 1, uses])
  40. check(id, paths, tforms_matrices, offsets, facecolors, edgecolors)
  41. check(id, paths[0:1], tforms_matrices, offsets, facecolors, edgecolors)
  42. check(id, [], tforms_matrices, offsets, facecolors, edgecolors)
  43. check(id, paths, tforms_matrices[0:1], offsets, facecolors, edgecolors)
  44. check(id, paths, [], offsets, facecolors, edgecolors)
  45. for n in range(0, offsets.shape[0]):
  46. check(id, paths, tforms_matrices, offsets[0:n, :],
  47. facecolors, edgecolors)
  48. check(id, paths, tforms_matrices, offsets, [], edgecolors)
  49. check(id, paths, tforms_matrices, offsets, facecolors, [])
  50. check(id, paths, tforms_matrices, offsets, [], [])
  51. check(id, paths, tforms_matrices, offsets, facecolors[0:1], edgecolors)
  52. def test_canvas_ctor():
  53. assert isinstance(FigureCanvasBase().figure, Figure)
  54. def test_get_default_filename():
  55. fig = plt.figure()
  56. assert fig.canvas.get_default_filename() == "Figure_1.png"
  57. fig.canvas.manager.set_window_title("0:1/2<3")
  58. assert fig.canvas.get_default_filename() == "0_1_2_3.png"
  59. def test_canvas_change():
  60. fig = plt.figure()
  61. # Replaces fig.canvas
  62. canvas = FigureCanvasBase(fig)
  63. # Should still work.
  64. plt.close(fig)
  65. assert not plt.fignum_exists(fig.number)
  66. @pytest.mark.backend('pdf')
  67. def test_non_gui_warning(monkeypatch):
  68. plt.subplots()
  69. monkeypatch.setenv("DISPLAY", ":999")
  70. with pytest.warns(UserWarning) as rec:
  71. plt.show()
  72. assert len(rec) == 1
  73. assert ('FigureCanvasPdf is non-interactive, and thus cannot be shown'
  74. in str(rec[0].message))
  75. with pytest.warns(UserWarning) as rec:
  76. plt.gcf().show()
  77. assert len(rec) == 1
  78. assert ('FigureCanvasPdf is non-interactive, and thus cannot be shown'
  79. in str(rec[0].message))
  80. def test_grab_clear():
  81. fig, ax = plt.subplots()
  82. fig.canvas.grab_mouse(ax)
  83. assert fig.canvas.mouse_grabber == ax
  84. fig.clear()
  85. assert fig.canvas.mouse_grabber is None
  86. @pytest.mark.parametrize(
  87. "x, y", [(42, 24), (None, 42), (None, None), (200, 100.01), (205.75, 2.0)])
  88. def test_location_event_position(x, y):
  89. # LocationEvent should cast its x and y arguments to int unless it is None.
  90. fig, ax = plt.subplots()
  91. canvas = FigureCanvasBase(fig)
  92. event = LocationEvent("test_event", canvas, x, y)
  93. if x is None:
  94. assert event.x is None
  95. else:
  96. assert event.x == int(x)
  97. assert isinstance(event.x, int)
  98. if y is None:
  99. assert event.y is None
  100. else:
  101. assert event.y == int(y)
  102. assert isinstance(event.y, int)
  103. if x is not None and y is not None:
  104. assert (ax.format_coord(x, y)
  105. == f"(x, y) = ({ax.format_xdata(x)}, {ax.format_ydata(y)})")
  106. ax.fmt_xdata = ax.fmt_ydata = lambda x: "foo"
  107. assert ax.format_coord(x, y) == "(x, y) = (foo, foo)"
  108. def test_location_event_position_twin():
  109. fig, ax = plt.subplots()
  110. ax.set(xlim=(0, 10), ylim=(0, 20))
  111. assert ax.format_coord(5., 5.) == "(x, y) = (5.00, 5.00)"
  112. ax.twinx().set(ylim=(0, 40))
  113. assert ax.format_coord(5., 5.) == "(x, y) = (5.00, 5.00) | (5.00, 10.0)"
  114. ax.twiny().set(xlim=(0, 5))
  115. assert (ax.format_coord(5., 5.)
  116. == "(x, y) = (5.00, 5.00) | (5.00, 10.0) | (2.50, 5.00)")
  117. def test_pick():
  118. fig = plt.figure()
  119. fig.text(.5, .5, "hello", ha="center", va="center", picker=True)
  120. fig.canvas.draw()
  121. picks = []
  122. def handle_pick(event):
  123. assert event.mouseevent.key == "a"
  124. picks.append(event)
  125. fig.canvas.mpl_connect("pick_event", handle_pick)
  126. KeyEvent("key_press_event", fig.canvas, "a")._process()
  127. MouseEvent("button_press_event", fig.canvas,
  128. *fig.transFigure.transform((.5, .5)),
  129. MouseButton.LEFT)._process()
  130. KeyEvent("key_release_event", fig.canvas, "a")._process()
  131. assert len(picks) == 1
  132. def test_interactive_zoom():
  133. fig, ax = plt.subplots()
  134. ax.set(xscale="logit")
  135. assert ax.get_navigate_mode() is None
  136. tb = NavigationToolbar2(fig.canvas)
  137. tb.zoom()
  138. assert ax.get_navigate_mode() == 'ZOOM'
  139. xlim0 = ax.get_xlim()
  140. ylim0 = ax.get_ylim()
  141. # Zoom from x=1e-6, y=0.1 to x=1-1e-5, 0.8 (data coordinates, "d").
  142. d0 = (1e-6, 0.1)
  143. d1 = (1-1e-5, 0.8)
  144. # Convert to screen coordinates ("s"). Events are defined only with pixel
  145. # precision, so round the pixel values, and below, check against the
  146. # corresponding xdata/ydata, which are close but not equal to d0/d1.
  147. s0 = ax.transData.transform(d0).astype(int)
  148. s1 = ax.transData.transform(d1).astype(int)
  149. # Zoom in.
  150. start_event = MouseEvent(
  151. "button_press_event", fig.canvas, *s0, MouseButton.LEFT)
  152. fig.canvas.callbacks.process(start_event.name, start_event)
  153. stop_event = MouseEvent(
  154. "button_release_event", fig.canvas, *s1, MouseButton.LEFT)
  155. fig.canvas.callbacks.process(stop_event.name, stop_event)
  156. assert ax.get_xlim() == (start_event.xdata, stop_event.xdata)
  157. assert ax.get_ylim() == (start_event.ydata, stop_event.ydata)
  158. # Zoom out.
  159. start_event = MouseEvent(
  160. "button_press_event", fig.canvas, *s1, MouseButton.RIGHT)
  161. fig.canvas.callbacks.process(start_event.name, start_event)
  162. stop_event = MouseEvent(
  163. "button_release_event", fig.canvas, *s0, MouseButton.RIGHT)
  164. fig.canvas.callbacks.process(stop_event.name, stop_event)
  165. # Absolute tolerance much less than original xmin (1e-7).
  166. assert ax.get_xlim() == pytest.approx(xlim0, rel=0, abs=1e-10)
  167. assert ax.get_ylim() == pytest.approx(ylim0, rel=0, abs=1e-10)
  168. tb.zoom()
  169. assert ax.get_navigate_mode() is None
  170. assert not ax.get_autoscalex_on() and not ax.get_autoscaley_on()
  171. def test_widgetlock_zoompan():
  172. fig, ax = plt.subplots()
  173. ax.plot([0, 1], [0, 1])
  174. fig.canvas.widgetlock(ax)
  175. tb = NavigationToolbar2(fig.canvas)
  176. tb.zoom()
  177. assert ax.get_navigate_mode() is None
  178. tb.pan()
  179. assert ax.get_navigate_mode() is None
  180. @pytest.mark.parametrize("plot_func", ["imshow", "contourf"])
  181. @pytest.mark.parametrize("orientation", ["vertical", "horizontal"])
  182. @pytest.mark.parametrize("tool,button,expected",
  183. [("zoom", MouseButton.LEFT, (4, 6)), # zoom in
  184. ("zoom", MouseButton.RIGHT, (-20, 30)), # zoom out
  185. ("pan", MouseButton.LEFT, (-2, 8)),
  186. ("pan", MouseButton.RIGHT, (1.47, 7.78))]) # zoom
  187. def test_interactive_colorbar(plot_func, orientation, tool, button, expected):
  188. fig, ax = plt.subplots()
  189. data = np.arange(12).reshape((4, 3))
  190. vmin0, vmax0 = 0, 10
  191. coll = getattr(ax, plot_func)(data, vmin=vmin0, vmax=vmax0)
  192. cb = fig.colorbar(coll, ax=ax, orientation=orientation)
  193. if plot_func == "contourf":
  194. # Just determine we can't navigate and exit out of the test
  195. assert not cb.ax.get_navigate()
  196. return
  197. assert cb.ax.get_navigate()
  198. # Mouse from 4 to 6 (data coordinates, "d").
  199. vmin, vmax = 4, 6
  200. # The y coordinate doesn't matter, it just needs to be between 0 and 1
  201. # However, we will set d0/d1 to the same y coordinate to test that small
  202. # pixel changes in that coordinate doesn't cancel the zoom like a normal
  203. # axes would.
  204. d0 = (vmin, 0.5)
  205. d1 = (vmax, 0.5)
  206. # Swap them if the orientation is vertical
  207. if orientation == "vertical":
  208. d0 = d0[::-1]
  209. d1 = d1[::-1]
  210. # Convert to screen coordinates ("s"). Events are defined only with pixel
  211. # precision, so round the pixel values, and below, check against the
  212. # corresponding xdata/ydata, which are close but not equal to d0/d1.
  213. s0 = cb.ax.transData.transform(d0).astype(int)
  214. s1 = cb.ax.transData.transform(d1).astype(int)
  215. # Set up the mouse movements
  216. start_event = MouseEvent(
  217. "button_press_event", fig.canvas, *s0, button)
  218. stop_event = MouseEvent(
  219. "button_release_event", fig.canvas, *s1, button)
  220. tb = NavigationToolbar2(fig.canvas)
  221. if tool == "zoom":
  222. tb.zoom()
  223. tb.press_zoom(start_event)
  224. tb.drag_zoom(stop_event)
  225. tb.release_zoom(stop_event)
  226. else:
  227. tb.pan()
  228. tb.press_pan(start_event)
  229. tb.drag_pan(stop_event)
  230. tb.release_pan(stop_event)
  231. # Should be close, but won't be exact due to screen integer resolution
  232. assert (cb.vmin, cb.vmax) == pytest.approx(expected, abs=0.15)
  233. def test_toolbar_zoompan():
  234. with pytest.warns(UserWarning, match=_EXPECTED_WARNING_TOOLMANAGER):
  235. plt.rcParams['toolbar'] = 'toolmanager'
  236. ax = plt.gca()
  237. fig = ax.get_figure()
  238. assert ax.get_navigate_mode() is None
  239. fig.canvas.manager.toolmanager.trigger_tool('zoom')
  240. assert ax.get_navigate_mode() == "ZOOM"
  241. fig.canvas.manager.toolmanager.trigger_tool('pan')
  242. assert ax.get_navigate_mode() == "PAN"
  243. def test_toolbar_home_restores_autoscale():
  244. fig, ax = plt.subplots()
  245. ax.plot(range(11), range(11))
  246. tb = NavigationToolbar2(fig.canvas)
  247. tb.zoom()
  248. # Switch to log.
  249. KeyEvent("key_press_event", fig.canvas, "k", 100, 100)._process()
  250. KeyEvent("key_press_event", fig.canvas, "l", 100, 100)._process()
  251. assert ax.get_xlim() == ax.get_ylim() == (1, 10) # Autolimits excluding 0.
  252. # Switch back to linear.
  253. KeyEvent("key_press_event", fig.canvas, "k", 100, 100)._process()
  254. KeyEvent("key_press_event", fig.canvas, "l", 100, 100)._process()
  255. assert ax.get_xlim() == ax.get_ylim() == (0, 10) # Autolimits.
  256. # Zoom in from (x, y) = (2, 2) to (5, 5).
  257. start, stop = ax.transData.transform([(2, 2), (5, 5)])
  258. MouseEvent("button_press_event", fig.canvas, *start, MouseButton.LEFT)._process()
  259. MouseEvent("button_release_event", fig.canvas, *stop, MouseButton.LEFT)._process()
  260. # Go back to home.
  261. KeyEvent("key_press_event", fig.canvas, "h")._process()
  262. assert ax.get_xlim() == ax.get_ylim() == (0, 10)
  263. # Switch to log.
  264. KeyEvent("key_press_event", fig.canvas, "k", 100, 100)._process()
  265. KeyEvent("key_press_event", fig.canvas, "l", 100, 100)._process()
  266. assert ax.get_xlim() == ax.get_ylim() == (1, 10) # Autolimits excluding 0.
  267. @pytest.mark.parametrize(
  268. "backend", ['svg', 'ps', 'pdf',
  269. pytest.param('pgf', marks=needs_pgf_xelatex)]
  270. )
  271. def test_draw(backend):
  272. from matplotlib.figure import Figure
  273. from matplotlib.backends.backend_agg import FigureCanvas
  274. test_backend = importlib.import_module(f'matplotlib.backends.backend_{backend}')
  275. TestCanvas = test_backend.FigureCanvas
  276. fig_test = Figure(constrained_layout=True)
  277. TestCanvas(fig_test)
  278. axes_test = fig_test.subplots(2, 2)
  279. # defaults to FigureCanvasBase
  280. fig_agg = Figure(constrained_layout=True)
  281. # put a backends.backend_agg.FigureCanvas on it
  282. FigureCanvas(fig_agg)
  283. axes_agg = fig_agg.subplots(2, 2)
  284. init_pos = [ax.get_position() for ax in axes_test.ravel()]
  285. fig_test.canvas.draw()
  286. fig_agg.canvas.draw()
  287. layed_out_pos_test = [ax.get_position() for ax in axes_test.ravel()]
  288. layed_out_pos_agg = [ax.get_position() for ax in axes_agg.ravel()]
  289. for init, placed in zip(init_pos, layed_out_pos_test):
  290. assert not np.allclose(init, placed, atol=0.005)
  291. for ref, test in zip(layed_out_pos_agg, layed_out_pos_test):
  292. np.testing.assert_allclose(ref, test, atol=0.005)
  293. @pytest.mark.parametrize(
  294. "key,mouseend,expectedxlim,expectedylim",
  295. [(None, (0.2, 0.2), (3.49, 12.49), (2.7, 11.7)),
  296. (None, (0.2, 0.5), (3.49, 12.49), (0, 9)),
  297. (None, (0.5, 0.2), (0, 9), (2.7, 11.7)),
  298. (None, (0.5, 0.5), (0, 9), (0, 9)), # No move
  299. (None, (0.8, 0.25), (-3.47, 5.53), (2.25, 11.25)),
  300. (None, (0.2, 0.25), (3.49, 12.49), (2.25, 11.25)),
  301. (None, (0.8, 0.85), (-3.47, 5.53), (-3.14, 5.86)),
  302. (None, (0.2, 0.85), (3.49, 12.49), (-3.14, 5.86)),
  303. ("shift", (0.2, 0.4), (3.49, 12.49), (0, 9)), # snap to x
  304. ("shift", (0.4, 0.2), (0, 9), (2.7, 11.7)), # snap to y
  305. ("shift", (0.2, 0.25), (3.49, 12.49), (3.49, 12.49)), # snap to diagonal
  306. ("shift", (0.8, 0.25), (-3.47, 5.53), (3.47, 12.47)), # snap to diagonal
  307. ("shift", (0.8, 0.9), (-3.58, 5.41), (-3.58, 5.41)), # snap to diagonal
  308. ("shift", (0.2, 0.85), (3.49, 12.49), (-3.49, 5.51)), # snap to diagonal
  309. ("x", (0.2, 0.1), (3.49, 12.49), (0, 9)), # only x
  310. ("y", (0.1, 0.2), (0, 9), (2.7, 11.7)), # only y
  311. ("control", (0.2, 0.2), (3.49, 12.49), (3.49, 12.49)), # diagonal
  312. ("control", (0.4, 0.2), (2.72, 11.72), (2.72, 11.72)), # diagonal
  313. ])
  314. def test_interactive_pan(key, mouseend, expectedxlim, expectedylim):
  315. fig, ax = plt.subplots()
  316. ax.plot(np.arange(10))
  317. assert ax.get_navigate()
  318. # Set equal aspect ratio to easier see diagonal snap
  319. ax.set_aspect('equal')
  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. # Set up the mouse movements
  328. start_event = MouseEvent(
  329. "button_press_event", fig.canvas, *sstart, button=MouseButton.LEFT,
  330. key=key)
  331. stop_event = MouseEvent(
  332. "button_release_event", fig.canvas, *send, button=MouseButton.LEFT,
  333. key=key)
  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)
  342. def test_toolmanager_remove():
  343. with pytest.warns(UserWarning, match=_EXPECTED_WARNING_TOOLMANAGER):
  344. plt.rcParams['toolbar'] = 'toolmanager'
  345. fig = plt.gcf()
  346. initial_len = len(fig.canvas.manager.toolmanager.tools)
  347. assert 'forward' in fig.canvas.manager.toolmanager.tools
  348. fig.canvas.manager.toolmanager.remove_tool('forward')
  349. assert len(fig.canvas.manager.toolmanager.tools) == initial_len - 1
  350. assert 'forward' not in fig.canvas.manager.toolmanager.tools
  351. def test_toolmanager_get_tool():
  352. with pytest.warns(UserWarning, match=_EXPECTED_WARNING_TOOLMANAGER):
  353. plt.rcParams['toolbar'] = 'toolmanager'
  354. fig = plt.gcf()
  355. rubberband = fig.canvas.manager.toolmanager.get_tool('rubberband')
  356. assert isinstance(rubberband, RubberbandBase)
  357. assert fig.canvas.manager.toolmanager.get_tool(rubberband) is rubberband
  358. with pytest.warns(UserWarning,
  359. match="ToolManager does not control tool 'foo'"):
  360. assert fig.canvas.manager.toolmanager.get_tool('foo') is None
  361. assert fig.canvas.manager.toolmanager.get_tool('foo', warn=False) is None
  362. with pytest.warns(UserWarning,
  363. match="ToolManager does not control tool 'foo'"):
  364. assert fig.canvas.manager.toolmanager.trigger_tool('foo') is None
  365. def test_toolmanager_update_keymap():
  366. with pytest.warns(UserWarning, match=_EXPECTED_WARNING_TOOLMANAGER):
  367. plt.rcParams['toolbar'] = 'toolmanager'
  368. fig = plt.gcf()
  369. assert 'v' in fig.canvas.manager.toolmanager.get_tool_keymap('forward')
  370. with pytest.warns(UserWarning,
  371. match="Key c changed from back to forward"):
  372. fig.canvas.manager.toolmanager.update_keymap('forward', 'c')
  373. assert fig.canvas.manager.toolmanager.get_tool_keymap('forward') == ['c']
  374. with pytest.raises(KeyError, match="'foo' not in Tools"):
  375. fig.canvas.manager.toolmanager.update_keymap('foo', 'c')
  376. @pytest.mark.parametrize("tool", ["zoom", "pan"])
  377. @pytest.mark.parametrize("button", [MouseButton.LEFT, MouseButton.RIGHT])
  378. @pytest.mark.parametrize("patch_vis", [True, False])
  379. @pytest.mark.parametrize("forward_nav", [True, False, "auto"])
  380. @pytest.mark.parametrize("t_s", ["twin", "share"])
  381. def test_interactive_pan_zoom_events(tool, button, patch_vis, forward_nav, t_s):
  382. # Bottom axes: ax_b Top axes: ax_t
  383. fig, ax_b = plt.subplots()
  384. ax_t = fig.add_subplot(221, zorder=99)
  385. ax_t.set_forward_navigation_events(forward_nav)
  386. ax_t.patch.set_visible(patch_vis)
  387. # ----------------------------
  388. if t_s == "share":
  389. ax_t_twin = fig.add_subplot(222)
  390. ax_t_twin.sharex(ax_t)
  391. ax_t_twin.sharey(ax_t)
  392. ax_b_twin = fig.add_subplot(223)
  393. ax_b_twin.sharex(ax_b)
  394. ax_b_twin.sharey(ax_b)
  395. elif t_s == "twin":
  396. ax_t_twin = ax_t.twinx()
  397. ax_b_twin = ax_b.twinx()
  398. # just some styling to simplify manual checks
  399. ax_t.set_label("ax_t")
  400. ax_t.patch.set_facecolor((1, 0, 0, 0.5))
  401. ax_t_twin.set_label("ax_t_twin")
  402. ax_t_twin.patch.set_facecolor("r")
  403. ax_b.set_label("ax_b")
  404. ax_b.patch.set_facecolor((0, 0, 1, 0.5))
  405. ax_b_twin.set_label("ax_b_twin")
  406. ax_b_twin.patch.set_facecolor("b")
  407. # ----------------------------
  408. # Set initial axis limits
  409. init_xlim, init_ylim = (0, 10), (0, 10)
  410. for ax in [ax_t, ax_b]:
  411. ax.set_xlim(*init_xlim)
  412. ax.set_ylim(*init_ylim)
  413. # Mouse from 2 to 1 (in data-coordinates of ax_t).
  414. xstart_t, xstop_t, ystart_t, ystop_t = 1, 2, 1, 2
  415. # Convert to screen coordinates ("s"). Events are defined only with pixel
  416. # precision, so round the pixel values, and below, check against the
  417. # corresponding xdata/ydata, which are close but not equal to s0/s1.
  418. s0 = ax_t.transData.transform((xstart_t, ystart_t)).astype(int)
  419. s1 = ax_t.transData.transform((xstop_t, ystop_t)).astype(int)
  420. # Calculate the mouse-distance in data-coordinates of the bottom-axes
  421. xstart_b, ystart_b = ax_b.transData.inverted().transform(s0)
  422. xstop_b, ystop_b = ax_b.transData.inverted().transform(s1)
  423. # Set up the mouse movements
  424. start_event = MouseEvent("button_press_event", fig.canvas, *s0, button)
  425. stop_event = MouseEvent("button_release_event", fig.canvas, *s1, button)
  426. tb = NavigationToolbar2(fig.canvas)
  427. if tool == "zoom":
  428. # Evaluate expected limits before executing the zoom-event
  429. direction = ("in" if button == 1 else "out")
  430. xlim_t, ylim_t = ax_t._prepare_view_from_bbox([*s0, *s1], direction)
  431. if ax_t.get_forward_navigation_events() is True:
  432. xlim_b, ylim_b = ax_b._prepare_view_from_bbox([*s0, *s1], direction)
  433. elif ax_t.get_forward_navigation_events() is False:
  434. xlim_b = init_xlim
  435. ylim_b = init_ylim
  436. else:
  437. if not ax_t.patch.get_visible():
  438. xlim_b, ylim_b = ax_b._prepare_view_from_bbox([*s0, *s1], direction)
  439. else:
  440. xlim_b = init_xlim
  441. ylim_b = init_ylim
  442. tb.zoom()
  443. tb.press_zoom(start_event)
  444. tb.drag_zoom(stop_event)
  445. tb.release_zoom(stop_event)
  446. assert ax_t.get_xlim() == pytest.approx(xlim_t, abs=0.15)
  447. assert ax_t.get_ylim() == pytest.approx(ylim_t, abs=0.15)
  448. assert ax_b.get_xlim() == pytest.approx(xlim_b, abs=0.15)
  449. assert ax_b.get_ylim() == pytest.approx(ylim_b, abs=0.15)
  450. # Check if twin-axes are properly triggered
  451. assert ax_t.get_xlim() == pytest.approx(ax_t_twin.get_xlim(), abs=0.15)
  452. assert ax_b.get_xlim() == pytest.approx(ax_b_twin.get_xlim(), abs=0.15)
  453. else:
  454. # Evaluate expected limits
  455. # (call start_pan to make sure ax._pan_start is set)
  456. ax_t.start_pan(*s0, button)
  457. xlim_t, ylim_t = ax_t._get_pan_points(button, None, *s1).T.astype(float)
  458. ax_t.end_pan()
  459. if ax_t.get_forward_navigation_events() is True:
  460. ax_b.start_pan(*s0, button)
  461. xlim_b, ylim_b = ax_b._get_pan_points(button, None, *s1).T.astype(float)
  462. ax_b.end_pan()
  463. elif ax_t.get_forward_navigation_events() is False:
  464. xlim_b = init_xlim
  465. ylim_b = init_ylim
  466. else:
  467. if not ax_t.patch.get_visible():
  468. ax_b.start_pan(*s0, button)
  469. xlim_b, ylim_b = ax_b._get_pan_points(button, None, *s1).T.astype(float)
  470. ax_b.end_pan()
  471. else:
  472. xlim_b = init_xlim
  473. ylim_b = init_ylim
  474. tb.pan()
  475. tb.press_pan(start_event)
  476. tb.drag_pan(stop_event)
  477. tb.release_pan(stop_event)
  478. assert ax_t.get_xlim() == pytest.approx(xlim_t, abs=0.15)
  479. assert ax_t.get_ylim() == pytest.approx(ylim_t, abs=0.15)
  480. assert ax_b.get_xlim() == pytest.approx(xlim_b, abs=0.15)
  481. assert ax_b.get_ylim() == pytest.approx(ylim_b, abs=0.15)
  482. # Check if twin-axes are properly triggered
  483. assert ax_t.get_xlim() == pytest.approx(ax_t_twin.get_xlim(), abs=0.15)
  484. assert ax_b.get_xlim() == pytest.approx(ax_b_twin.get_xlim(), abs=0.15)