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

Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions lib/matplotlib/axes/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import matplotlib.text as mtext
import matplotlib.ticker as mticker
import matplotlib.transforms as mtransforms
import matplotlib.collections as mcollections

_log = logging.getLogger(__name__)

Expand Down Expand Up @@ -2605,6 +2606,10 @@ def relim(self, visible_only=False):
self._update_patch_limits(artist)
elif isinstance(artist, mimage.AxesImage):
self._update_image_limits(artist)
elif isinstance(artist, mcollections.Collection):
offsets = artist.get_offsets()
if offsets is not None and len(offsets):
self.update_datalim(offsets)
Comment thread
timhoffm marked this conversation as resolved.
Outdated

def update_datalim(self, xys, updatex=True, updatey=True):
"""
Expand Down
23 changes: 23 additions & 0 deletions lib/matplotlib/tests/test_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -10159,3 +10159,26 @@

mocked_im_draw.assert_not_called()
mocked_ln_draw.assert_not_called()

def test_relim_updates_scatter_offsets():

Check warning on line 10163 in lib/matplotlib/tests/test_axes.py

View workflow job for this annotation

GitHub Actions / ruff

[rdjson] reported by reviewdog 🐶 Expected 2 blank lines, found 1 Raw Output: message:"Expected 2 blank lines, found 1" location:{path:"/home/runner/work/matplotlib/matplotlib/lib/matplotlib/tests/test_axes.py" range:{start:{line:10163 column:1} end:{line:10163 column:4}}} severity:WARNING source:{name:"ruff" url:"https://docs.astral.sh/ruff"} code:{value:"E302" url:"https://docs.astral.sh/ruff/rules/blank-lines-top-level"} suggestions:{range:{start:{line:10162 column:1} end:{line:10163 column:1}} text:"\n\n"}
import numpy as np
import matplotlib.pyplot as plt

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