Skip to content

FIX: Handle no-offsets in collection datalim #22945

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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
30 changes: 16 additions & 14 deletions lib/matplotlib/collections.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,8 +195,9 @@ def __init__(self,

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

if offsets is not None:
if self._has_offsets:
offsets = np.asanyarray(offsets, float)
# Broadcast (2,) -> (1, 2) but nothing else.
if offsets.shape == (2,):
Expand Down Expand Up @@ -278,7 +279,7 @@ def get_datalim(self, transData):
offsets = offsets.filled(np.nan)
# get_path_collection_extents handles nan but not masked arrays

if len(paths) and len(offsets):
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 +
Expand All @@ -290,18 +291,19 @@ def get_datalim(self, transData):
offset_trf.transform_non_affine(offsets),
offset_trf.get_affine().frozen())

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

def get_window_extent(self, renderer):
Expand Down
12 changes: 12 additions & 0 deletions lib/matplotlib/tests/test_collections.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import matplotlib.pyplot as plt
import matplotlib.collections as mcollections
import matplotlib.colors as mcolors
import matplotlib.path as mpath
import matplotlib.transforms as mtransforms
from matplotlib.collections import (Collection, LineCollection,
EventCollection, PolyCollection)
Expand Down Expand Up @@ -291,6 +292,17 @@ def test_null_collection_datalim():
mtransforms.Bbox.null().get_points())


def test_no_offsets_datalim():
# A collection with no offsets and a non transData
# transform should return a null bbox
ax = plt.axes()
coll = mcollections.PathCollection([mpath.Path([(0, 0), (1, 0)])])
ax.add_collection(coll)
coll_data_lim = coll.get_datalim(mtransforms.IdentityTransform())
assert_array_equal(coll_data_lim.get_points(),
mtransforms.Bbox.null().get_points())


def test_add_collection():
# Test if data limits are unchanged by adding an empty collection.
# GitHub issue #1490, pull #1497.
Expand Down