8000 MNT: Use get_offsets() to handle the default/None case · matplotlib/matplotlib@e953840 · GitHub
[go: up one dir, main page]

Skip to content

Commit e953840

Browse files
committed
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.
1 parent de4a94c commit e953840

File tree

1 file changed

+44
-48
lines changed

1 file changed

+44
-48
lines changed

lib/matplotlib/collections.py

Lines changed: 44 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@ class Collection(artist.Artist, cm.ScalarMappable):
6262
mappable will be used to set the ``facecolors`` and ``edgecolors``,
6363
ignoring those that were manually passed in.
6464
"""
65-
_offsets = np.zeros((0, 2))
6665
#: Either a list of 3x3 arrays or an Nx3x3 array (representing N
6766
#: transforms), suitable for the `all_transforms` argument to
6867
#: `~matplotlib.backend_bases.RendererBase.draw_path_collection`;
@@ -193,16 +192,12 @@ def __init__(self,
193192
else:
194193
self._joinstyle = None
195194

196-
# default to zeros
197-
self._offsets = np.zeros((1, 2))
198-
self._has_offsets = offsets is not None
199-
200-
if self._has_offsets:
195+
if offsets is not None:
201196
offsets = np.asanyarray(offsets, float)
202197
# Broadcast (2,) -> (1, 2) but nothing else.
203198
if offsets.shape == (2,):
204199
offsets = offsets[None, :]
205-
self._offsets = offsets
200+
self._offsets = offsets
206201

207202
self._offset_transform = offset_transform
208203

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

269264
paths = self.get_paths()
265+
if not len(paths):
266+
# No paths to transform
267+
return transforms.Bbox.null()
270268

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

278-
if isinstance(offsets, np.ma.MaskedArray):
279-
offsets = offsets.filled(np.nan)
280-
# get_path_collection_extents handles nan but not masked arrays
281-
282-
if len(paths):
283-
if any(transform.contains_branch_seperately(transData)):
284-
# collections that are just in data units (like quiver)
285-
# can properly have the axes limits set by their shape +
286-
# offset. LineCollections that have no offsets can
287-
# also use this algorithm (like streamplot).
288-
return mpath.get_path_collection_extents(
289-
transform.get_affine() - transData, paths,
290-
self.get_transforms(),
291-
offset_trf.transform_non_affine(offsets),
292-
offset_trf.get_affine().frozen())
293-
294-
if self._has_offsets:
295-
# this is for collections that have their paths (shapes)
296-
# in physical, axes-relative, or figure-relative units
297-
# (i.e. like scatter). We can't uniquely set limits based on
298-
# those shapes, so we just set the limits based on their
299-
# location.
300-
offsets = (offset_trf - transData).transform(offsets)
301-
# note A-B means A B^{-1}
302-
offsets = np.ma.masked_invalid(offsets)
303-
if not offsets.mask.all():
304-
bbox = transforms.Bbox.null()
305-
bbox.update_from_data_xy(offsets)
306-
return bbox
276+
if any(transform.contains_branch_seperately(transData)):
277+
# collections that are just in data units (like quiver)
278+
# can properly have the axes limits set by their shape +
279+
# offset. LineCollections that have no offsets can
280+
# also use this algorithm (like streamplot).
281+
if isinstance(offsets, np.ma.MaskedArray):
282+
offsets = offsets.filled(np.nan)
283+
# get_path_collection_extents handles nan but not masked arrays
284+
return mpath.get_path_collection_extents(
285+
transform.get_affine() - transData, paths,
286+
self.get_transforms(),
287+
offset_trf.transform_non_affine(offsets),
288+
offset_trf.get_affine().frozen())
289+
290+
# NOTE: None is the default case where no offsets were passed in
291+
if self._offsets is not None:
292+
# this is for collections that have their paths (shapes)
293+
# in physical, axes-relative, or figure-relative units
294+
# (i.e. like scatter). We can't uniquely set limits based on
295+
# those shapes, so we just set the limits based on their
296+
# location.
297+
offsets = (offset_trf - transData).transform(offsets)
298+
# note A-B means A B^{-1}
299+
offsets = np.ma.masked_invalid(offsets)
300+
if not offsets.mask.all():
301+
bbox = transforms.Bbox.null()
302+
bbox.update_from_data_xy(offsets)
303+
return bbox
307304
return transforms.Bbox.null()
308305

309306
def get_window_extent(self, renderer):
@@ -316,7 +313,7 @@ def _prepare_points(self):
316313

317314
transform = self.get_transform()
318315
offset_trf = self.get_offset_transform()
319-
offsets = self._offsets
316+
offsets = self.get_offsets()
320317
paths = self.get_paths()
321318

322319
if self.have_units():
@@ -327,10 +324,9 @@ def _prepare_points(self):
327324
xs = self.convert_xunits(xs)
328325
ys = self.convert_yunits(ys)
329326
paths.append(mpath.Path(np.column_stack([xs, ys]), path.codes))
330-
if offsets.size:
331-
xs = self.convert_xunits(offsets[:, 0])
332-
ys = self.convert_yunits(offsets[:, 1])
333-
offsets = np.column_stack([xs, ys])
327+
xs = self.convert_xunits(offsets[:, 0])
328+
ys = self.convert_yunits(offsets[:, 1])
329+
offsets = np.column_stack([xs, ys])
334330

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

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

564561
def _get_default_linewidth(self):
565562
# This may be overridden in a subclass.
@@ -2156,13 +2153,12 @@ def draw(self, renderer):
21562153
renderer.open_group(self.__class__.__name__, self.get_gid())
21572154
transform = self.get_transform()
21582155
offset_trf = self.get_offset_transform()
2159-
offsets = self._offsets
2156+
offsets = self.get_offsets()
21602157

21612158
if self.have_units():
2162-
if len(self._offsets):
2163-
xs = self.convert_xunits(self._offsets[:, 0])
2164-
ys = self.convert_yunits(self._offsets[:, 1])
2165-
offsets = np.column_stack([xs, ys])
2159+
xs = self.convert_xunits(offsets[:, 0])
2160+
ys = self.convert_yunits(offsets[:, 1])
2161+
offsets = np.column_stack([xs, ys])
21662162

21672163
self.update_scalarmappable()
21682164

0 commit comments

Comments
 (0)
0