Skip to content

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

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 1 commit into from
Feb 18, 2022
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
33 changes: 16 additions & 17 deletions lib/matplotlib/collections.py
Original file line number Diff line number Diff line change
Expand Up @@ -258,9 +258,9 @@ def get_datalim(self, transData):

transform = self.get_transform()
offset_trf = self.get_offset_transform()
has_offsets = np.any(self._offsets) # True if any non-zero offsets
if has_offsets and not offset_trf.contains_branch(transData):
# if there are offsets but in some coords other than data,
if not (isinstance(offset_trf, transforms.IdentityTransform)
or offset_trf.contains_branch(transData)):
# if the offsets are in some coords other than data,
# then don't use them for autoscaling.
return transforms.Bbox.null()
Copy link
Member

Choose a reason for hiding this comment

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

I am very wary of just ripping out a block of code like this without justification. The original issue was that the autoscaling logic was faulty when the offsets was basically null. But this block is for a special case where the offset transform isn't the usual transform and that we can't usually calculate the autoscale correctly in that case. Just ripping this out doesn't address this special case.

Copy link
Member

Choose a reason for hiding this comment

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

I am also highly suspicious that ripping this block out did not trigger a test failure

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the push to investigate this, Ben. You were right to be wary of this passing without any tests.

I added a new test that adds a scatter in transAxes coordinates (fixing the point in Axes space). Which I think we agree should not contribute to autoscaling. When I ripped this chunk of code out, it would have contributed.

I did however change the conditional to be: "if offsets is an updated transform (not Identity), and does not contain the transData path". Rather than checking for that possibly Falsy "has_offsets".

offsets = self._offsets
Expand Down Expand Up @@ -289,20 +289,19 @@ def get_datalim(self, transData):
self.get_transforms(),
offset_trf.transform_non_affine(offsets),
offset_trf.get_affine().frozen())
if has_offsets:
# 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 = (offset_trf - 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

Copy link
Contributor

Choose a reason for hiding this comment

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

Should the check for has_offsets a few lines earlier also be dropped? (if offset_trf is "anything", it certainly can map (0, 0) to anywhere and therefore autoscaling based on the collection probably doesn't make sense either.

Regardless of this comment, the PR as is fixes the original issue; this is just about a more general case.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

👍 I got rid of that entire block above.

# 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 = (offset_trf - 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 @@ -711,6 +711,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