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

Skip to content

[fix] Spine.set_bounds() does not take parameter **None** as expected #30330

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 18 additions & 7 deletions lib/matplotlib/spines.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,20 +232,31 @@ def _clear(self):
"""
self._position = None # clear position

def _adjust_location(self):
"""Automatically set spine bounds to the view interval."""

if self.spine_type == 'circle':
return
def _get_bounds_or_viewLim(self):
"""
Get the bounds of the spine.

If self._bounds is None, return self.axes.viewLim.intervalx
or self.axes.viewLim.intervaly based on self.spine_type
"""
if self._bounds is not None:
low, high = self._bounds
elif self.spine_type in ('left', 'right'):
low, high = self.axes.viewLim.intervaly
elif self.spine_type in ('top', 'bottom'):
low, high = self.axes.viewLim.intervalx
else:
raise ValueError(f'unknown spine spine_type: {self.spine_type}')
raise ValueError(f'spine_type: {self.spine_type} not supported')
# circle not supported
return low, high

def _adjust_location(self):
"""Automatically set spine bounds to the view interval."""

if self.spine_type == 'circle':
return

low, high = self._get_bounds_or_viewLim()

if self._patch_type == 'arc':
if self.spine_type in ('bottom', 'top'):
Expand Down Expand Up @@ -424,7 +435,7 @@ def set_bounds(self, low=None, high=None):
'set_bounds() method incompatible with circular spines')
if high is None and np.iterable(low):
low, high = low
old_low, old_high = self.get_bounds() or (None, None)
old_low, old_high = self._get_bounds_or_viewLim()
if low is None:
low = old_low
if high is None:
Expand Down
29 changes: 29 additions & 0 deletions lib/matplotlib/tests/test_spines.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,3 +166,32 @@ def test_arc_spine_inner_no_axis():
assert ax.spines["inner"].axis is None

fig.draw_without_rendering()


def test_spine_set_bounds_with_none():
"""Test that set_bounds(None, ...) uses original axis view limits."""
fig, ax = plt.subplots()

# Plot some data to set axis limits
x = np.linspace(0, 10, 100)
y = np.sin(x)
ax.plot(x, y)

xlim = ax.viewLim.intervalx
ylim = ax.viewLim.intervaly
# Use modified set_bounds with None
ax.spines['bottom'].set_bounds(0, None)
ax.spines['left'].set_bounds(None, None)

# Check that get_bounds returns correct numeric values
bottom_bound = ax.spines['bottom'].get_bounds()
left_bound = ax.spines['left'].get_bounds()
left_bounds_not_None = (left_bound[0] is not None) and (left_bound[1] is not None)

assert bottom_bound[1] is not None, "Higher bound should be numeric"
assert left_bounds_not_None, "left bound should be numeric"
assert np.isclose(bottom_bound[0], 0), "Lower bound should match provided value"
assert np.isclose(bottom_bound[1],
xlim[1]), "Upper bound should match original value"
assert np.isclose(left_bound[0], ylim[0]), "Lower bound should match original value"
assert np.isclose(left_bound[1], ylim[1]), "Upper bound should match original value"
Loading