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

Skip to content

Commit 3da28af

Browse files
committed
BUG: Preserve autolim=False semantics for Collection relim support
_set_in_autoscale(True) was set unconditionally in add_collection(), outside the 'if autolim:' block. This meant that any collection added with autolim=False would still be picked up by relim() later. Fix: move _set_in_autoscale(True) inside the 'if autolim:' block so that relim() only considers collections that explicitly opted in. Add two regression tests: - test_relim_collection_autolim_false: verifies that a collection added with autolim=False does not affect limits after relim(). - test_relim_collection_log_scale: verifies that relim() + autoscale_view() works correctly for a Collection on log-scaled axes (exercises the minpos path). Closes #30859
1 parent 1d8b229 commit 3da28af

2 files changed

Lines changed: 38 additions & 1 deletion

File tree

lib/matplotlib/axes/_base.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2432,9 +2432,12 @@ def add_collection(self, collection, autolim=True):
24322432
)
24332433
if autolim != "_datalim_only":
24342434
self._request_autoscale_view()
2435+
# Mark collection as participating in relim() only when autolim
2436+
# is enabled. If autolim=False the caller explicitly opted out,
2437+
# so relim() must not pick this collection up later.
2438+
collection._set_in_autoscale(True)
24352439

24362440
self.stale = True
2437-
collection._set_in_autoscale(True)
24382441
return collection
24392442

24402443
def add_image(self, image):

lib/matplotlib/tests/test_axes.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6542,6 +6542,40 @@ def test_relim_collection():
65426542
assert ylim[1] < 10
65436543

65446544

6545+
def test_relim_collection_autolim_false():
6546+
# GH#30859 - Collection added with autolim=False must not participate
6547+
# in relim() later.
6548+
import matplotlib.collections as mcollections
6549+
fig, ax = plt.subplots()
6550+
ax.set_xlim(0, 1)
6551+
ax.set_ylim(0, 1)
6552+
# Build a collection far outside current limits and add it with autolim=False.
6553+
sc = mcollections.PathCollection([])
6554+
sc.set_offsets([[100, 200], [300, 400]])
6555+
ax.add_collection(sc, autolim=False)
6556+
ax.relim()
6557+
ax.autoscale_view()
6558+
# Limits must remain unchanged because autolim=False was requested.
6559+
assert ax.get_xlim() == (0, 1)
6560+
assert ax.get_ylim() == (0, 1)
6561+
6562+
6563+
def test_relim_collection_log_scale():
6564+
# GH#30859 - relim() for Collection on a log-scaled axis should
6565+
# correctly pick up minpos so that log scaling works properly.
6566+
fig, ax = plt.subplots()
6567+
ax.set_xscale('log')
6568+
ax.set_yscale('log')
6569+
sc = ax.scatter([1e-3, 1e-2, 1e-1], [1e1, 1e2, 1e3])
6570+
sc.set_offsets([[1e1, 1e4], [1e2, 1e5]])
6571+
ax.relim()
6572+
ax.autoscale_view()
6573+
xlim = ax.get_xlim()
6574+
ylim = ax.get_ylim()
6575+
assert xlim[0] <= 1e1 and xlim[1] >= 1e2
6576+
assert ylim[0] <= 1e4 and ylim[1] >= 1e5
6577+
6578+
65456579
def test_text_labelsize():
65466580
"""
65476581
tests for issue #1172

0 commit comments

Comments
 (0)