Skip to content

Conversation

Copy link

Copilot AI commented Aug 31, 2025

The SpanSelector widget was incorrectly expanding axes limits to include 0 when created with interactive=True, even when the plotted data didn't include 0. This regression affected the expected behavior where axes limits should remain focused on the actual data range.

Problem

When creating a SpanSelector with interactive=True:

from matplotlib import pyplot as plt
from matplotlib.widgets import SpanSelector

fig, ax = plt.subplots()
ax.plot([10, 20], [10, 20])  # Data range: 10-20
ss = SpanSelector(ax, print, "horizontal", interactive=True)
print(ax.get_xlim())  # Expected: ~(10, 20), Actual: ~(0, 20)

The axes xlimits were being expanded to include x=0, which was not the case in Matplotlib 3.4 with the old span_stays parameter.

Root Cause

When interactive=True, SpanSelector creates interactive handles via ToolLineHandles at initialization. These handles were positioned using self.extents, which returns (0, 0) from the initially created Rectangle at origin. This caused axvline(0) calls that forced the axes to include x=0, even though the handles were invisible.

Solution

Modified _setup_edge_handle to use current axes limits (ax.get_xlim() or ax.get_ylim()) as initial handle positions instead of self.extents. This prevents unwanted axes expansion while maintaining all interactive functionality.

The key change ensures handles are created within existing axes bounds:

# Before: uses (0, 0) positions
self._edge_handles = ToolLineHandles(self.ax, self.extents, ...)

# After: uses existing axes limits  
if self.direction == 'horizontal':
    initial_positions = self.ax.get_xlim()
else:
    initial_positions = self.ax.get_ylim()
self._edge_handles = ToolLineHandles(self.ax, initial_positions, ...)

After user interaction, handles are updated to correct positions via the normal extents.setter mechanism, preserving all existing behavior.

Fixes #1.


💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.

Copy link
Author

Copilot AI commented Aug 31, 2025

@srodif 👋 This repository doesn't have Copilot instructions. With Copilot instructions, I can understand the repository better, work faster and produce higher quality PRs.

I can generate a .github/copilot-instructions.md file for you automatically. Click here to open a pre-filled issue and assign it to me. I'll write the instructions, and then tag you for review.

- Modified _setup_edge_handle to use current axes limits as initial handle positions
- Prevents axvline/axhline calls at position 0 that force axes expansion
- Added test to verify fix works correctly
- Maintains all existing functionality while fixing the regression

Co-authored-by: srodif <91968331+srodif@users.noreply.github.com>
@sonarqubecloud
Copy link

Copilot AI changed the title [WIP] interactive SpanSelector incorrectly forces axes limits to include 0 Fix SpanSelector interactive mode forcing axes limits to include 0 Aug 31, 2025
Copilot AI requested a review from srodif August 31, 2025 20:07
Copilot finished work on behalf of srodif August 31, 2025 20:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

interactive SpanSelector incorrectly forces axes limits to include 0

2 participants