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

Skip to content

Commit 529d37c

Browse files
committed
Fix relim() to update limits for scatter PathCollection and add regression test
1 parent 08fe8bc commit 529d37c

2 files changed

Lines changed: 28 additions & 0 deletions

File tree

lib/matplotlib/axes/_base.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import matplotlib.text as mtext
3030
import matplotlib.ticker as mticker
3131
import matplotlib.transforms as mtransforms
32+
import matplotlib.collections as mcollections
3233

3334
_log = logging.getLogger(__name__)
3435

@@ -2605,6 +2606,10 @@ def relim(self, visible_only=False):
26052606
self._update_patch_limits(artist)
26062607
elif isinstance(artist, mimage.AxesImage):
26072608
self._update_image_limits(artist)
2609+
elif isinstance(artist, mcollections.Collection):
2610+
offsets = artist.get_offsets()
2611+
if offsets is not None and len(offsets):
2612+
self.update_datalim(offsets)
26082613

26092614
def update_datalim(self, xys, updatex=True, updatey=True):
26102615
"""

lib/matplotlib/tests/test_axes.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10159,3 +10159,26 @@ def test_animated_artists_not_drawn_by_default():
1015910159

1016010160
mocked_im_draw.assert_not_called()
1016110161
mocked_ln_draw.assert_not_called()
10162+
10163+
def test_relim_updates_scatter_offsets():
10164+
import numpy as np
10165+
import matplotlib.pyplot as plt
10166+
10167+
fig, ax = plt.subplots()
10168+
10169+
xs = np.linspace(0, 10, 100)
10170+
ys = np.sin(xs)
10171+
scatter = ax.scatter(xs, ys)
10172+
10173+
# Shift scatter upward
10174+
new_ys = np.sin(xs) + 5
10175+
scatter.set_offsets(np.column_stack((xs, new_ys)))
10176+
10177+
ax.relim()
10178+
ax.autoscale_view()
10179+
10180+
ymin, ymax = ax.get_ylim()
10181+
10182+
# New limits should reflect shifted data
10183+
assert ymin > 3
10184+
assert ymax > 5

0 commit comments

Comments
 (0)