From 7b34fc5c0bcbcb2d637b9d11cf1f9caaf0298e90 Mon Sep 17 00:00:00 2001 From: Thomas A Caswell Date: Thu, 17 Feb 2022 23:09:15 -0500 Subject: [PATCH] Backport PR #22476: FIX: Include (0, 0) offsets in scatter autoscaling --- lib/matplotlib/collections.py | 32 ++++++++++++------------ lib/matplotlib/tests/test_collections.py | 16 ++++++++++++ 2 files changed, 32 insertions(+), 16 deletions(-) diff --git a/lib/matplotlib/collections.py b/lib/matplotlib/collections.py index e7e630475e0d..e45d8527a83c 100644 --- a/lib/matplotlib/collections.py +++ b/lib/matplotlib/collections.py @@ -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() @@ -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): diff --git a/lib/matplotlib/tests/test_collections.py b/lib/matplotlib/tests/test_collections.py index fb7dade2b459..583d1489a668 100644 --- a/lib/matplotlib/tests/test_collections.py +++ b/lib/matplotlib/tests/test_collections.py @@ -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, {}),