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

Skip to content

Commit 91e8ea1

Browse files
authored
Merge pull request #22476 from greglucas/fix-scatter-zeros
FIX: Include (0, 0) offsets in scatter autoscaling
2 parents 88813fc + f93a0fc commit 91e8ea1

File tree

2 files changed

+32
-17
lines changed

2 files changed

+32
-17
lines changed

lib/matplotlib/collections.py

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -258,9 +258,9 @@ def get_datalim(self, transData):
258258

259259
transform = self.get_transform()
260260
offset_trf = self.get_offset_transform()
261-
has_offsets = np.any(self._offsets) # True if any non-zero offsets
262-
if has_offsets and not offset_trf.contains_branch(transData):
263-
# if there are offsets but in some coords other than data,
261+
if not (isinstance(offset_trf, transforms.IdentityTransform)
262+
or offset_trf.contains_branch(transData)):
263+
# if the offsets are in some coords other than data,
264264
# then don't use them for autoscaling.
265265
return transforms.Bbox.null()
266266
offsets = self._offsets
@@ -289,20 +289,19 @@ def get_datalim(self, transData):
289289
self.get_transforms(),
290290
offset_trf.transform_non_affine(offsets),
291291
offset_trf.get_affine().frozen())
292-
if has_offsets:
293-
# this is for collections that have their paths (shapes)
294-
# in physical, axes-relative, or figure-relative units
295-
# (i.e. like scatter). We can't uniquely set limits based on
296-
# those shapes, so we just set the limits based on their
297-
# location.
298-
299-
offsets = (offset_trf - transData).transform(offsets)
300-
# note A-B means A B^{-1}
301-
offsets = np.ma.masked_invalid(offsets)
302-
if not offsets.mask.all():
303-
bbox = transforms.Bbox.null()
304-
bbox.update_from_data_xy(offsets)
305-
return bbox
292+
293+
# this is for collections that have their paths (shapes)
294+
# in physical, axes-relative, or figure-relative units
295+
# (i.e. like scatter). We can't uniquely set limits based on
296+
# those shapes, so we just set the limits based on their
297+
# location.
298+
offsets = (offset_trf - transData).transform(offsets)
299+
# note A-B means A B^{-1}
300+
offsets = np.ma.masked_invalid(offsets)
301+
if not offsets.mask.all():
302+
bbox = transforms.Bbox.null()
303+
bbox.update_from_data_xy(offsets)
304+
return bbox
306305
return transforms.Bbox.null()
307306

308307
def get_window_extent(self, renderer):

lib/matplotlib/tests/test_collections.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -711,6 +711,22 @@ def test_singleton_autolim():
711711
np.testing.assert_allclose(ax.get_xlim(), [-0.06, 0.06])
712712

713713

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

0 commit comments

Comments
 (0)