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

Skip to content

Commit c29bcec

Browse files
committed
TST: Adding interactive colorbar tests
Adding tests for vertical and horizontal placements, zoom in, zoom out, and pan. Also verifying that a colorbar on a Contourset is not able to be interacted with.
1 parent e25a898 commit c29bcec

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed

lib/matplotlib/tests/test_backend_bases.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,63 @@ def test_interactive_zoom():
181181
assert not ax.get_autoscalex_on() and not ax.get_autoscaley_on()
182182

183183

184+
@pytest.mark.parametrize("plot_func", ["imshow", "contourf"])
185+
@pytest.mark.parametrize("orientation", ["vertical", "horizontal"])
186+
@pytest.mark.parametrize("tool,button,expected",
187+
[("zoom", MouseButton.LEFT, (4, 6)), # zoom in
188+
("zoom", MouseButton.RIGHT, (-20, 30)), # zoom out
189+
("pan", MouseButton.LEFT, (-2, 8))])
190+
def test_interactive_colorbar(plot_func, orientation, tool, button, expected):
191+
fig, ax = plt.subplots()
192+
data = np.arange(12).reshape((4, 3))
193+
vmin0, vmax0 = 0, 10
194+
coll = getattr(ax, plot_func)(data, vmin=vmin0, vmax=vmax0)
195+
196+
cb = fig.colorbar(coll, ax=ax, orientation=orientation)
197+
if plot_func == "contourf":
198+
# Just determine we can't navigate and exit out of the test
199+
assert not cb.ax.get_navigate()
200+
return
201+
202+
assert cb.ax.get_navigate()
203+
204+
# Mouse from 4 to 6 (data coordinates, "d").
205+
# The y coordinate doesn't matter, just needs to be between 0 and 1
206+
vmin, vmax = 4, 6
207+
d0 = (vmin, 0.1)
208+
d1 = (vmax, 0.9)
209+
# Swap them if the orientation is vertical
210+
if orientation == "vertical":
211+
d0 = d0[::-1]
212+
d1 = d1[::-1]
213+
# Convert to screen coordinates ("s"). Events are defined only with pixel
214+
# precision, so round the pixel values, and below, check against the
215+
# corresponding xdata/ydata, which are close but not equal to d0/d1.
216+
s0 = cb.ax.transData.transform(d0).astype(int)
217+
s1 = cb.ax.transData.transform(d1).astype(int)
218+
219+
# Set up the mouse movements
220+
start_event = MouseEvent(
221+
"button_press_event", fig.canvas, *s0, button)
222+
stop_event = MouseEvent(
223+
"button_release_event", fig.canvas, *s1, button)
224+
225+
tb = NavigationToolbar2(fig.canvas)
226+
if tool == "zoom":
227+
tb.zoom()
228+
tb.press_zoom(start_event)
229+
tb.drag_zoom(stop_event)
230+
tb.release_zoom(stop_event)
231+
else:
232+
tb.pan()
233+
tb.press_pan(start_event)
234+
tb.drag_pan(stop_event)
235+
tb.release_pan(stop_event)
236+
237+
# Should be close, but won't be exact due to screen integer resolution
238+
assert (cb.vmin, cb.vmax) == pytest.approx(expected, abs=0.15)
239+
240+
184241
def test_toolbar_zoompan():
185242
expected_warning_regex = (
186243
r"Treat the new Tool classes introduced in "

0 commit comments

Comments
 (0)