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

Skip to content
55 changes: 30 additions & 25 deletions lib/matplotlib/axes/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -1133,6 +1133,31 @@ def _update_transScale(self):
mtransforms.blended_transform_factory(
self.xaxis.get_transform(), self.yaxis.get_transform()))

def _update_collection_limits(self, collection, autolim):
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did you put the method in exactly this location?

This needs a docstring.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

okay I have shifted the _update_collection_limits after understanding other update function location .

self._unstale_viewLim()

datalim = collection.get_datalim(self.transData)
points = datalim.get_points()

if not np.isinf(datalim.minpos).all():
points = np.concatenate([points, [datalim.minpos]])

x_is_data, y_is_data = (
collection.get_transform().contains_branch_separately(self.transData)
)
ox_is_data, oy_is_data = (
collection.get_offset_transform().contains_branch_separately(self.transData)
)

self.update_datalim(
points,
updatex=x_is_data or ox_is_data,
updatey=y_is_data or oy_is_data,
)

if autolim != "_datalim_only":
self._request_autoscale_view()

Comment thread
timhoffm marked this conversation as resolved.
Outdated
def get_position(self, original=False):
"""
Return the position of the Axes within the figure as a `.Bbox`.
Expand Down Expand Up @@ -2393,32 +2418,10 @@ def add_collection(self, collection, autolim=True):
if collection.get_clip_path() is None:
collection.set_clip_path(self.patch)

collection._set_in_autoscale(autolim)

if autolim:
# Make sure viewLim is not stale (mostly to match
# pre-lazy-autoscale behavior, which is not really better).
self._unstale_viewLim()
datalim = collection.get_datalim(self.transData)
points = datalim.get_points()
if not np.isinf(datalim.minpos).all():
# By definition, if minpos (minimum positive value) is set
# (i.e., non-inf), then min(points) <= minpos <= max(points),
# and minpos would be superfluous. However, we add minpos to
# the call so that self.dataLim will update its own minpos.
# This ensures that log scales see the correct minimum.
points = np.concatenate([points, [datalim.minpos]])
# only update the dataLim for x/y if the collection uses transData
# in this direction.
x_is_data, y_is_data = (collection.get_transform()
.contains_branch_separately(self.transData))
ox_is_data, oy_is_data = (collection.get_offset_transform()
.contains_branch_separately(self.transData))
self.update_datalim(
points,
updatex=x_is_data or ox_is_data,
updatey=y_is_data or oy_is_data,
)
if autolim != "_datalim_only":
self._request_autoscale_view()
self._update_collection_limits(collection, autolim)

self.stale = True
return collection
Expand Down Expand Up @@ -2649,6 +2652,8 @@ def relim(self, visible_only=False):
self._update_patch_limits(artist)
elif isinstance(artist, mimage.AxesImage):
self._update_image_limits(artist)
elif isinstance(artist, mcoll.Collection):
self._update_collection_limits(artist, autolim="_datalim_only")

def update_datalim(self, xys, updatex=True, updatey=True):
"""
Expand Down
21 changes: 21 additions & 0 deletions lib/matplotlib/tests/test_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -10189,3 +10189,24 @@ def test_errorbar_uses_rcparams():
assert_allclose([cap.get_markeredgewidth() for cap in caplines], 2.5)
for barcol in barlinecols:
assert_allclose(barcol.get_linewidths(), 1.75)


def test_relim_updates_scatter_offsets():
fig, ax = plt.subplots()

xs = np.linspace(0, 10, 100)
ys = np.sin(xs)
scatter = ax.scatter(xs, ys)

# Shift scatter upward
new_ys = np.sin(xs) + 5
scatter.set_offsets(np.column_stack((xs, new_ys)))

ax.relim()
ax.autoscale_view()

ymin, ymax = ax.get_ylim()

# New limits should reflect shifted data
assert ymin > 3
assert ymax > 5
Loading