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

Skip to content

Commit cb8bce5

Browse files
committed
BUG: Fix relim() to support Collection artists (scatter, etc.) relim() previously skipped Collection instances entirely. Two issues: 1. add_collection() did not call collection._set_in_autoscale(True), unlike add_line(), add_patch(), and add_image(). This caused relim() to skip collections via the _get_in_autoscale() check. 2. relim() had no handling for Collection instances. Added an elif branch that calls get_datalim() and update_datalim(), mirroring the logic already used in add_collection(). Closes #30859
1 parent dde0763 commit cb8bce5

2 files changed

Lines changed: 24 additions & 4 deletions

File tree

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
``relim()`` now accounts for Collection artists
2+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3+
Previously, `~.axes.Axes.relim` did not recalculate data limits for
4+
`.Collection` artists (e.g. those created by `~.axes.Axes.scatter`).
5+
Calling ``ax.relim()`` followed by ``ax.autoscale_view()`` now correctly
6+
includes scatter plots and other collections in the axes limits.

lib/matplotlib/axes/_base.py

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2434,6 +2434,7 @@ def add_collection(self, collection, autolim=True):
24342434
self._request_autoscale_view()
24352435

24362436
self.stale = True
2437+
collection._set_in_autoscale(True)
24372438
return collection
24382439

24392440
def add_image(self, image):
@@ -2638,15 +2639,11 @@ def relim(self, visible_only=False):
26382639
"""
26392640
Recompute the data limits based on current artists.
26402641
2641-
At present, `.Collection` instances are not supported.
2642-
26432642
Parameters
26442643
----------
26452644
visible_only : bool, default: False
26462645
Whether to exclude invisible artists.
26472646
"""
2648-
# Collections are deliberately not supported (yet); see
2649-
# the TODO note in artists.py.
26502647
self.dataLim.ignore(True)
26512648
self.dataLim.set_points(mtransforms.Bbox.null().get_points())
26522649
self.ignore_existing_data_limits = True
@@ -2661,6 +2658,23 @@ def relim(self, visible_only=False):
26612658
self._update_patch_limits(artist)
26622659
elif isinstance(artist, mimage.AxesImage):
26632660
self._update_image_limits(artist)
2661+
elif isinstance(artist, mcoll.Collection):
2662+
datalim = artist.get_datalim(self.transData)
2663+
points = datalim.get_points()
2664+
if not np.isinf(datalim.minpos).all():
2665+
points = np.concatenate([points,
2666+
[datalim.minpos]])
2667+
x_is_data, y_is_data = (
2668+
artist.get_transform()
2669+
.contains_branch_separately(self.transData))
2670+
ox_is_data, oy_is_data = (
2671+
artist.get_offset_transform()
2672+
.contains_branch_separately(self.transData))
2673+
self.update_datalim(
2674+
points,
2675+
updatex=x_is_data or ox_is_data,
2676+
updatey=y_is_data or oy_is_data,
2677+
)
26642678

26652679
def update_datalim(self, xys, updatex=True, updatey=True):
26662680
"""

0 commit comments

Comments
 (0)