Skip to content

Commit 7b34fc5

Browse files
tacaswellgreglucas
authored andcommitted
Backport PR #22476: FIX: Include (0, 0) offsets in scatter autoscaling
1 parent a8a868a commit 7b34fc5

File tree

2 files changed

+32
-16
lines changed

2 files changed

+32
-16
lines changed

lib/matplotlib/collections.py

+16-16
Original file line numberDiff line numberDiff line change
@@ -268,8 +268,8 @@ def get_datalim(self, transData):
268268

269269
transform = self.get_transform()
270270
transOffset = self.get_offset_transform()
271-
hasOffsets = np.any(self._offsets) # True if any non-zero offsets
272-
if hasOffsets and not transOffset.contains_branch(transData):
271+
if not (isinstance(transOffset, transforms.IdentityTransform)
272+
or transOffset.contains_branch(transData)):
273273
# if there are offsets but in some coords other than data,
274274
# then don't use them for autoscaling.
275275
return transforms.Bbox.null()
@@ -299,20 +299,20 @@ def get_datalim(self, transData):
299299
self.get_transforms(),
300300
transOffset.transform_non_affine(offsets),
301301
transOffset.get_affine().frozen())
302-
if hasOffsets:
303-
# this is for collections that have their paths (shapes)
304-
# in physical, axes-relative, or figure-relative units
305-
# (i.e. like scatter). We can't uniquely set limits based on
306-
# those shapes, so we just set the limits based on their
307-
# location.
308-
309-
offsets = (transOffset - transData).transform(offsets)
310-
# note A-B means A B^{-1}
311-
offsets = np.ma.masked_invalid(offsets)
312-
if not offsets.mask.all():
313-
bbox = transforms.Bbox.null()
314-
bbox.update_from_data_xy(offsets)
315-
return bbox
302+
303+
# this is for collections that have their paths (shapes)
304+
# in physical, axes-relative, or figure-relative units
305+
# (i.e. like scatter). We can't uniquely set limits based on
306+
# those shapes, so we just set the limits based on their
307+
# location.
308+
309+
offsets = (transOffset - transData).transform(offsets)
310+
# note A-B means A B^{-1}
311+
offsets = np.ma.masked_invalid(offsets)
312+
if not offsets.mask.all():
313+
bbox = transforms.Bbox.null()
314+
bbox.update_from_data_xy(offsets)
315+
return bbox
316316
return transforms.Bbox.null()
317317

318318
def get_window_extent(self, renderer):

lib/matplotlib/tests/test_collections.py

+16
Original file line numberDiff line numberDiff line change
@@ -715,6 +715,22 @@ def test_singleton_autolim():
715715
np.testing.assert_allclose(ax.get_xlim(), [-0.06, 0.06])
716716

717717

718+
@pytest.mark.parametrize("transform, expected", [
719+
("transData", (-0.5, 3.5)),
720+
("transAxes", (2.8, 3.2)),
721+
])
722+
def test_autolim_with_zeros(transform, expected):
723+
# 1) Test that a scatter at (0, 0) data coordinates contributes to
724+
# autoscaling even though any(offsets) would be False in that situation.
725+
# 2) Test that specifying transAxes for the transform does not contribute
726+
# to the autoscaling.
727+
fig, ax = plt.subplots()
728+
ax.scatter(0, 0, transform=getattr(ax, transform))
729+
ax.scatter(3, 3)
730+
np.testing.assert_allclose(ax.get_ylim(), expected)
731+
np.testing.assert_allclose(ax.get_xlim(), expected)
732+
733+
718734
@pytest.mark.parametrize('flat_ref, kwargs', [
719735
(True, {}),
720736
(False, {}),

0 commit comments

Comments
 (0)