Skip to content

Backport PR #22476: FIX: Include (0, 0) offsets in scatter autoscaling #22492

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
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
32 changes: 16 additions & 16 deletions lib/matplotlib/collections.py
Original file line number Diff line number Diff line change
Expand Up @@ -268,8 +268,8 @@ def get_datalim(self, transData):

transform = self.get_transform()
transOffset = self.get_offset_transform()
hasOffsets = np.any(self._offsets) # True if any non-zero offsets
if hasOffsets and not transOffset.contains_branch(transData):
if not (isinstance(transOffset, transforms.IdentityTransform)
or transOffset.contains_branch(transData)):
# if there are offsets but in some coords other than data,
# then don't use them for autoscaling.
return transforms.Bbox.null()
Expand Down Expand Up @@ -299,20 +299,20 @@ def get_datalim(self, transData):
self.get_transforms(),
transOffset.transform_non_affine(offsets),
transOffset.get_affine().frozen())
if hasOffsets:
# this is for collections that have their paths (shapes)
# in physical, axes-relative, or figure-relative units
# (i.e. like scatter). We can't uniquely set limits based on
# those shapes, so we just set the limits based on their
# location.

offsets = (transOffset - transData).transform(offsets)
# note A-B means A B^{-1}
offsets = np.ma.masked_invalid(offsets)
if not offsets.mask.all():
bbox = transforms.Bbox.null()
bbox.update_from_data_xy(offsets)
return bbox

# this is for collections that have their paths (shapes)
# in physical, axes-relative, or figure-relative units
# (i.e. like scatter). We can't uniquely set limits based on
# those shapes, so we just set the limits based on their
# location.

offsets = (transOffset - transData).transform(offsets)
# note A-B means A B^{-1}
offsets = np.ma.masked_invalid(offsets)
if not offsets.mask.all():
bbox = transforms.Bbox.null()
bbox.update_from_data_xy(offsets)
return bbox
return transforms.Bbox.null()

def get_window_extent(self, renderer):
Expand Down
16 changes: 16 additions & 0 deletions lib/matplotlib/tests/test_collections.py
Original file line number Diff line number Diff line change
Expand Up @@ -715,6 +715,22 @@ def test_singleton_autolim():
np.testing.assert_allclose(ax.get_xlim(), [-0.06, 0.06])


@pytest.mark.parametrize("transform, expected", [
("transData", (-0.5, 3.5)),
("transAxes", (2.8, 3.2)),
])
def test_autolim_with_zeros(transform, expected):
# 1) Test that a scatter at (0, 0) data coordinates contributes to
# autoscaling even though any(offsets) would be False in that situation.
# 2) Test that specifying transAxes for the transform does not contribute
# to the autoscaling.
fig, ax = plt.subplots()
ax.scatter(0, 0, transform=getattr(ax, transform))
ax.scatter(3, 3)
np.testing.assert_allclose(ax.get_ylim(), expected)
np.testing.assert_allclose(ax.get_xlim(), expected)


@pytest.mark.parametrize('flat_ref, kwargs', [
(True, {}),
(False, {}),
Expand Down