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

Skip to content

Commit f90c147

Browse files
committed
TST/BUG: Fix _datalim_only semantics and add user-facing relim test
_set_in_autoscale(True) must not be called for collections added with autolim='_datalim_only' — that sentinel means 'update datalim once but do not enter the autoscale/relim system'. Previously, because the call was outside the if autolim: block, bool('_datalim_only') == True meant 3D collections inadvertently got _in_autoscale=True. Fixed by gating the call on autolim != '_datalim_only', consistent with the existing _request_autoscale_view guard directly below. Also add test_relim_collection_autoscale_view: an end-to-end regression that checks ax.get_xlim()/get_ylim() after relim()+autoscale_view(), matching the exact user-facing scenario from GH#30859.
1 parent a1c58ec commit f90c147

2 files changed

Lines changed: 18 additions & 1 deletion

File tree

lib/matplotlib/axes/_base.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2418,7 +2418,8 @@ def add_collection(self, collection, autolim=True):
24182418
collection.set_clip_path(self.patch)
24192419

24202420
if autolim:
2421-
collection._set_in_autoscale(True)
2421+
if autolim != "_datalim_only":
2422+
collection._set_in_autoscale(True)
24222423
# Make sure viewLim is not stale (mostly to match
24232424
# pre-lazy-autoscale behavior, which is not really better).
24242425
self._unstale_viewLim()

lib/matplotlib/tests/test_axes.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6568,6 +6568,22 @@ def test_relim_collection_log_scale():
65686568
assert_allclose(ax.dataLim.minpos, expected.minpos)
65696569

65706570

6571+
def test_relim_collection_autoscale_view():
6572+
# GH#30859 - end-to-end: after set_offsets(), relim() + autoscale_view()
6573+
# must update the visible axis limits, not just dataLim.
6574+
fig, ax = plt.subplots()
6575+
sc = ax.scatter([], [])
6576+
xs = np.linspace(0, 10, 50)
6577+
sc.set_offsets(np.column_stack((xs, np.sin(xs))))
6578+
ax.relim()
6579+
ax.autoscale_view()
6580+
xlim = ax.get_xlim()
6581+
ylim = ax.get_ylim()
6582+
# autoscale_view adds a margin, so limits should comfortably contain data
6583+
assert xlim[0] <= 0 and xlim[1] >= 10, f"xlim should contain [0, 10], got {xlim}"
6584+
assert ylim[0] <= -1 and ylim[1] >= 1, f"ylim should contain [-1, 1], got {ylim}"
6585+
6586+
65716587
def test_text_labelsize():
65726588
"""
65736589
tests for issue #1172

0 commit comments

Comments
 (0)