10000 FIX: Handle no-offsets in collection datalim · matplotlib/matplotlib@de4a94c · GitHub
[go: up one dir, main page]

Skip to content

Commit de4a94c

Browse files
committed
FIX: Handle no-offsets in collection datalim
This adds a check for whether the collection has offsets passed in initially. This is necessary to handle the (0, 0) offset case that is desired and distinguishing that from the initial empty offset of (0, 0). In particular, this is necessary for the non-transData IdentityTransform passed in during tight_layout calculations.
1 parent 89b21b5 commit de4a94c

File tree

2 files changed

+28
-14
lines changed

2 files changed

+28
-14
lines changed

lib/matplotlib/collections.py

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -195,8 +195,9 @@ def __init__(self,
195195

196196
# default to zeros
197197
self._offsets = np.zeros((1, 2))
198+
self._has_offsets = offsets is not None
198199

199-
if offsets is not None:
200+
if self._has_offsets:
200201
offsets = np.asanyarray(offsets, float)
201202
# Broadcast (2,) -> (1, 2) but nothing else.
202203
if offsets.shape == (2,):
@@ -278,7 +279,7 @@ def get_datalim(self, transData):
278279
offsets = offsets.filled(np.nan)
279280
# get_path_collection_extents handles nan but not masked arrays
280281

281-
if len(paths) and len(offsets):
282+
if len(paths):
282283
if any(transform.contains_branch_seperately(transData)):
283284
# collections that are just in data units (like quiver)
284285
# can properly have the axes limits set by their shape +
@@ -290,18 +291,19 @@ def get_datalim(self, transData):
290291
offset_trf.transform_non_affine(offsets),
291292
offset_trf.get_affine().frozen())
292293

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
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
305307
return transforms.Bbox.null()
306308

307309
def get_window_extent(self, renderer):

lib/matplotlib/tests/test_collections.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import matplotlib.pyplot as plt
1010
import matplotlib.collections as mcollections
1111
import matplotlib.colors as mcolors
12+
import matplotlib.path as mpath
1213
import matplotlib.transforms as mtransforms
1314
from matplotlib.collections import (Collection, LineCollection,
1415
EventCollection, PolyCollection)
@@ -291,6 +292,17 @@ def test_null_collection_datalim():
291292
mtransforms.Bbox.null().get_points())
292293

293294

295+
def test_no_offsets_datalim():
296+
# A collection with no offsets and a non transData
297+
# transform should return a null bbox
298+
ax = plt.axes()
299+
coll = mcollections.PathCollection([mpath.Path([(0, 0), (1, 0)])])
300+
ax.add_collection(coll)
301+
coll_data_lim = coll.get_datalim(mtransforms.IdentityTransform())
302+
assert_array_equal(coll_data_lim.get_points(),
303+
mtransforms.Bbox.null().get_points())
304+
305+
294306
def test_add_collection():
295307
# Test if data limits are unchanged by adding an empty collection.
296308
# GitHub issue #1490, pull #1497.

0 commit comments

Comments
 (0)
0