diff --git a/lib/matplotlib/axes/_axes.py b/lib/matplotlib/axes/_axes.py index affc329f1be0..ac62f2cd37ef 100644 --- a/lib/matplotlib/axes/_axes.py +++ b/lib/matplotlib/axes/_axes.py @@ -4478,6 +4478,8 @@ def scatter(self, x, y, s=None, c=None, marker=None, cmap=None, norm=None, if self._ymargin < 0.05 and x.size > 0: self.set_ymargin(0.05) + self.update_datalim(collection.get_datalim_simple(self.transData)) + self.autoscale_view() self.add_collection(collection) self.autoscale_view() diff --git a/lib/matplotlib/collections.py b/lib/matplotlib/collections.py index 53df43a41a45..54c09853998b 100644 --- a/lib/matplotlib/collections.py +++ b/lib/matplotlib/collections.py @@ -178,19 +178,29 @@ def get_offset_transform(self): t = t._as_mpl_transform(self.axes) return t + def get_datalim_simple(self, transData): + offsets = self._offsets + transOffset = self.get_offset_transform() + offsets = np.asarray(transOffset.transform(offsets)) + minx = offsets[:, 0].min() + maxx = offsets[:, 0].max() + miny = offsets[:, 1].min() + maxy = offsets[:, 1].max() + bb = transforms.Bbox([[minx, miny], [maxx, maxy]]) + return bb.inverse_transformed(transData) + + def get_datalim(self, transData): transform = self.get_transform() transOffset = self.get_offset_transform() offsets = self._offsets paths = self.get_paths() - if not transform.is_affine: paths = [transform.transform_path_non_affine(p) for p in paths] transform = transform.get_affine() if not transOffset.is_affine: offsets = transOffset.transform_non_affine(offsets) transOffset = transOffset.get_affine() - if isinstance(offsets, np.ma.MaskedArray): offsets = offsets.filled(np.nan) # get_path_collection_extents handles nan but not masked arrays @@ -365,6 +375,9 @@ def contains(self, mouseevent): self._picker is not True # the bool, not just nonzero or 1 else self._pickradius) + if self.axes and self.get_offset_position() == "data": + self.axes._unstale_viewLim() + transform, transOffset, offsets, paths = self._prepare_points() ind = _path.point_in_path_collection(