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

Skip to content

Commit 2bc1243

Browse files
authored
Merge pull request #20676 from ericpre/fix_bound_span_selector
Fix bounds when initialising `SpanSelector`
2 parents a35a51f + 5c08ff6 commit 2bc1243

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

lib/matplotlib/tests/test_widgets.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,35 @@ def test_tool_line_handle():
303303
assert tool_line_handle.positions == positions
304304

305305

306+
@pytest.mark.parametrize('direction', ("horizontal", "vertical"))
307+
def test_span_selector_bound(direction):
308+
fig, ax = plt.subplots(1, 1)
309+
ax.plot([10, 20], [10, 30])
310+
ax.figure.canvas.draw()
311+
x_bound = ax.get_xbound()
312+
y_bound = ax.get_ybound()
313+
314+
tool = widgets.SpanSelector(ax, print, direction, interactive=True)
315+
assert ax.get_xbound() == x_bound
316+
assert ax.get_ybound() == y_bound
317+
318+
bound = x_bound if direction == 'horizontal' else y_bound
319+
assert tool._edge_handles.positions == list(bound)
320+
321+
press_data = [10.5, 11.5]
322+
move_data = [11, 13] # Updating selector is done in onmove
323+
release_data = move_data
324+
do_event(tool, 'press', xdata=press_data[0], ydata=press_data[1], button=1)
325+
do_event(tool, 'onmove', xdata=move_data[0], ydata=move_data[1], button=1)
326+
327+
assert ax.get_xbound() == x_bound
328+
assert ax.get_ybound() == y_bound
329+
330+
index = 0 if direction == 'horizontal' else 1
331+
handle_positions = [press_data[index], release_data[index]]
332+
assert tool._edge_handles.positions == handle_positions
333+
334+
306335
def check_lasso_selector(**kwargs):
307336
ax = get_ax()
308337

lib/matplotlib/widgets.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2163,7 +2163,12 @@ def new_axes(self, ax):
21632163
self.artists.append(self._rect)
21642164

21652165
def _setup_edge_handle(self, props):
2166-
self._edge_handles = ToolLineHandles(self.ax, self.extents,
2166+
# Define initial position using the axis bounds to keep the same bounds
2167+
if self.direction == 'horizontal':
2168+
positions = self.ax.get_xbound()
2169+
else:
2170+
positions = self.ax.get_ybound()
2171+
self._edge_handles = ToolLineHandles(self.ax, positions,
21672172
direction=self.direction,
21682173
line_props=props,
21692174
useblit=self.useblit)

0 commit comments

Comments
 (0)