Skip to content
Merged
Changes from 1 commit
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
Prev Previous commit
MNT: Use get_offsets() to handle the default/None case
This avoids carrying around an extra offsetsNone/has_offsets variable
to keep track of the default case. Instead calling get_offsets() to
return the default zeros case, and then internally checking the
None/default case in get_datalim() which is the only place where this
information is needed.
  • Loading branch information
greglucas committed May 1, 2022
commit e95384083dd2319a1b720789244c68c8d1c516bb
92 changes: 44 additions & 48 deletions lib/matplotlib/collections.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ class Collection(artist.Artist, cm.ScalarMappable):
mappable will be used to set the ``facecolors`` and ``edgecolors``,
ignoring those that were manually passed in.
"""
_offsets = np.zeros((0, 2))
#: Either a list of 3x3 arrays or an Nx3x3 array (representing N
#: transforms), suitable for the `all_transforms` argument to
#: `~matplotlib.backend_bases.RendererBase.draw_path_collection`;
Expand Down Expand Up @@ -193,16 +192,12 @@ def __init__(self,
else:
self._joinstyle = None

# default to zeros
self._offsets = np.zeros((1, 2))
self._has_offsets = offsets is not None

if self._has_offsets:
if offsets is not None:
offsets = np.asanyarray(offsets, float)
# Broadcast (2,) -> (1, 2) but nothing else.
if offsets.shape == (2,):
offsets = offsets[None, :]
self._offsets = offsets
self._offsets = offsets

self._offset_transform = offset_transform

Expand Down Expand Up @@ -264,9 +259,12 @@ def get_datalim(self, transData):
# if the offsets are in some coords other than data,
# then don't use them for autoscaling.
return transforms.Bbox.null()
offsets = self._offsets
offsets = self.get_offsets()

paths = self.get_paths()
if not len(paths):
# No paths to transform
return transforms.Bbox.null()

if not transform.is_affine:
paths = [transform.transform_path_non_affine(p) for p in paths]
Expand All @@ -275,35 +273,34 @@ def get_datalim(self, transData):
# transforms.get_affine().contains_branch(transData). But later,
# be careful to only apply the affine part that remains.

if isinstance(offsets, np.ma.MaskedArray):
offsets = offsets.filled(np.nan)
# get_path_collection_extents handles nan but not masked arrays

if len(paths):
if any(transform.contains_branch_seperately(transData)):
# collections that are just in data units (like quiver)
# can properly have the axes limits set by their shape +
# offset. LineCollections that have no offsets can
# also use this algorithm (like streamplot).
return mpath.get_path_collection_extents(
transform.get_affine() - transData, paths,
self.get_transforms(),
offset_trf.transform_non_affine(offsets),
offset_trf.get_affine().frozen())

if self._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
if any(transform.contains_branch_seperately(transData)):
# collections that are just in data units (like quiver)
# can properly have the axes limits set by their shape +
# offset. LineCollections that have no offsets can
# also use this algorithm (like streamplot).
if isinstance(offsets, np.ma.MaskedArray):
offsets = offsets.filled(np.nan)
# get_path_collection_extents handles nan but not masked arrays
return mpath.get_path_collection_extents(
transform.get_affine() - transData, paths,
self.get_transforms(),
offset_trf.transform_non_affine(offsets),
offset_trf.get_affine().frozen())

# NOTE: None is the default case where no offsets were passed in
if self._offsets is not None:
# 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 All @@ -316,7 +313,7 @@ def _prepare_points(self):

transform = self.get_transform()
offset_trf = self.get_offset_transform()
offsets = self._offsets
offsets = self.get_offsets()
paths = self.get_paths()

if self.have_units():
Expand All @@ -327,10 +324,9 @@ def _prepare_points(self):
xs = self.convert_xunits(xs)
ys = self.convert_yunits(ys)
paths.append(mpath.Path(np.column_stack([xs, ys]), path.codes))
if offsets.size:
xs = self.convert_xunits(offsets[:, 0])
ys = self.convert_yunits(offsets[:, 1])
offsets = np.column_stack([xs, ys])
xs = self.convert_xunits(offsets[:, 0])
ys = self.convert_yunits(offsets[:, 1])
offsets = np.column_stack([xs, ys])

if not transform.is_affine:
paths = [transform.transform_path_non_affine(path)
Expand Down Expand Up @@ -559,7 +555,8 @@ def set_offsets(self, offsets):

def get_offsets(self):
"""Return the offsets for the collection."""
return self._offsets
# Default to zeros in the no-offset (None) case
return np.zeros((1, 2)) if self._offsets is None else self._offsets

def _get_default_linewidth(self):
# This may be overridden in a subclass.
Expand Down Expand Up @@ -2156,13 +2153,12 @@ def draw(self, renderer):
renderer.open_group(self.__class__.__name__, self.get_gid())
transform = self.get_transform()
offset_trf = self.get_offset_transform()
offsets = self._offsets
offsets = self.get_offsets()

if self.have_units():
if len(self._offsets):
xs = self.convert_xunits(self._offsets[:, 0])
ys = self.convert_yunits(self._offsets[:, 1])
offsets = np.column_stack([xs, ys])
xs = self.convert_xunits(offsets[:, 0])
ys = self.convert_yunits(offsets[:, 1])
offsets = np.column_stack([xs, ys])

self.update_scalarmappable()

Expand Down