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

Skip to content

Commit bfdb6c9

Browse files
committed
TST/DOC: align collection relim tests and autolim semantics
1 parent 68a6269 commit bfdb6c9

3 files changed

Lines changed: 26 additions & 35 deletions

File tree

lib/matplotlib/artist.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -240,9 +240,6 @@ def remove(self):
240240
with `.FigureCanvasBase.draw_idle`. Call `~.axes.Axes.relim` to
241241
update the Axes limits if desired.
242242
243-
Note: `~.axes.Axes.relim` will not see collections even if the
244-
collection was added to the Axes with *autolim* = True.
245-
246243
Note: there is no support for removing the artist's legend entry.
247244
"""
248245

lib/matplotlib/axes/_base.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2384,7 +2384,10 @@ def add_collection(self, collection, autolim=True):
23842384
collection : `.Collection`
23852385
The collection to add.
23862386
autolim : bool
2387-
Whether to update data and view limits.
2387+
Whether to update data limits and request autoscaling.
2388+
2389+
If *False*, the collection is explicitly excluded from
2390+
`~.Axes.relim`.
23882391
23892392
.. versionchanged:: 3.11
23902393
@@ -2406,17 +2409,17 @@ def add_collection(self, collection, autolim=True):
24062409
if collection.get_clip_path() is None:
24072410
collection.set_clip_path(self.patch)
24082411

2412+
# Keep relim() participation aligned with the autolim argument.
2413+
# autolim can also be the internal sentinel "_datalim_only".
2414+
collection._set_in_autoscale(bool(autolim))
2415+
24092416
if autolim:
24102417
# Make sure viewLim is not stale (mostly to match
24112418
# pre-lazy-autoscale behavior, which is not really better).
24122419
self._unstale_viewLim()
24132420
self._update_collection_limits(collection)
24142421
if autolim != "_datalim_only":
24152422
self._request_autoscale_view()
2416-
# Mark collection as participating in relim() only when autolim
2417-
# is enabled. If autolim=False the caller explicitly opted out,
2418-
# so relim() must not pick this collection up later.
2419-
collection._set_in_autoscale(True)
24202423

24212424
self.stale = True
24222425
return collection

lib/matplotlib/tests/test_axes.py

Lines changed: 18 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -6515,65 +6515,56 @@ def test_relim_collection():
65156515
fig, ax = plt.subplots()
65166516
sc = ax.scatter([1, 2, 3], [4, 5, 6])
65176517
ax.relim()
6518-
ax.autoscale_view()
6519-
xlim = ax.get_xlim()
6520-
ylim = ax.get_ylim()
6521-
assert xlim[0] <= 1 and xlim[1] >= 3
6522-
assert ylim[0] <= 4 and ylim[1] >= 6
6518+
expected = sc.get_datalim(ax.transData)
6519+
assert_allclose(ax.dataLim.get_points(), expected.get_points())
6520+
assert_allclose(ax.dataLim.minpos, expected.minpos)
65236521

65246522
# After updating offsets, relim should track the new data.
65256523
sc.set_offsets([[10, 20], [30, 40]])
65266524
ax.relim()
6527-
ax.autoscale_view()
6528-
xlim = ax.get_xlim()
6529-
ylim = ax.get_ylim()
6530-
assert xlim[0] <= 10 and xlim[1] >= 30
6531-
assert ylim[0] <= 20 and ylim[1] >= 40
6525+
expected = sc.get_datalim(ax.transData)
6526+
assert_allclose(ax.dataLim.get_points(), expected.get_points())
6527+
assert_allclose(ax.dataLim.minpos, expected.minpos)
65326528

65336529
# visible_only=True should ignore hidden collections.
65346530
line, = ax.plot([0, 1], [0, 1])
65356531
sc.set_visible(False)
65366532
ax.relim(visible_only=True)
6537-
ax.autoscale_view()
6538-
xlim = ax.get_xlim()
6539-
ylim = ax.get_ylim()
65406533
# With scatter hidden, limits should be driven by the line only.
6541-
assert xlim[1] < 10
6542-
assert ylim[1] < 10
6534+
assert_allclose(ax.dataLim.get_points(), [[0, 0], [1, 1]])
6535+
assert_array_equal(ax.dataLim.minpos, [np.inf, np.inf])
65436536

65446537

65456538
def test_relim_collection_autolim_false():
65466539
# GH#30859 - Collection added with autolim=False must not participate
65476540
# in relim() later.
65486541
import matplotlib.collections as mcollections
65496542
fig, ax = plt.subplots()
6550-
ax.set_xlim(0, 1)
6551-
ax.set_ylim(0, 1)
6543+
ax.plot([0, 1], [0, 1])
6544+
ax.relim()
6545+
expected = ax.dataLim.frozen()
65526546
# Build a collection far outside current limits and add it with autolim=False.
65536547
sc = mcollections.PathCollection([])
65546548
sc.set_offsets([[100, 200], [300, 400]])
65556549
ax.add_collection(sc, autolim=False)
65566550
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)
6551+
# dataLim must remain unchanged because autolim=False was requested.
6552+
assert_allclose(ax.dataLim.get_points(), expected.get_points())
6553+
assert_allclose(ax.dataLim.minpos, expected.minpos)
65616554

65626555

65636556
def test_relim_collection_log_scale():
65646557
# GH#30859 - relim() for Collection on a log-scaled axis should
6565-
# correctly pick up minpos so that log scaling works properly.
6558+
# correctly propagate minpos into dataLim.
65666559
fig, ax = plt.subplots()
65676560
ax.set_xscale('log')
65686561
ax.set_yscale('log')
65696562
sc = ax.scatter([1e-3, 1e-2, 1e-1], [1e1, 1e2, 1e3])
65706563
sc.set_offsets([[1e1, 1e4], [1e2, 1e5]])
65716564
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
6565+
expected = sc.get_datalim(ax.transData)
6566+
assert_allclose(ax.dataLim.get_points(), expected.get_points())
6567+
assert_allclose(ax.dataLim.minpos, expected.minpos)
65776568

65786569

65796570
def test_text_labelsize():

0 commit comments

Comments
 (0)