Skip to content

allow setting collection offsets to empty #22408

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

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
12 changes: 8 additions & 4 deletions lib/matplotlib/collections.py
Original file line number Diff line number Diff line change
Expand Up @@ -549,11 +549,15 @@ def set_offsets(self, offsets):
offsets : (N, 2) or (2,) array-like
"""
offsets = np.asanyarray(offsets)
if offsets.shape == (2,): # Broadcast (2,) -> (1, 2) but nothing else.
if offsets.shape == (2,):
offsets = offsets[None, :]
self._offsets = np.column_stack(
(np.asarray(self.convert_xunits(offsets[:, 0]), 'float'),
np.asarray(self.convert_yunits(offsets[:, 1]), 'float')))

if offsets.shape == (2, 0):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if offsets.shape == (2, 0):
if offsets.shape == (0, 2):

Which will require some changes to the tests.

The docs say(N, 2) so this should be the degenerate case of ((N, 2), .., (2, 2), (1, 2), (0, 2)) (the (2,) case is a special case for spelling (1, 2)).

self._offsets = offsets.astype('float').T
else:
self._offsets = np.column_stack(
(np.asarray(self.convert_xunits(offsets[:, 0]), 'float'),
np.asarray(self.convert_yunits(offsets[:, 1]), 'float')))
self.stale = True

def get_offsets(self):
Expand Down
10 changes: 10 additions & 0 deletions lib/matplotlib/tests/test_collections.py
Original file line number Diff line number Diff line change
Expand Up @@ -1101,3 +1101,13 @@ def test_set_offset_units():
off0 = sc.get_offsets()
sc.set_offsets(list(zip(y, d)))
np.testing.assert_allclose(off0, sc.get_offsets())


def test_set_offset_empty():
expected = mcollections.Collection(offsets=np.column_stack([[], []]))

modified = mcollections.Collection(
offsets=np.column_stack([[0, 1], [0, 1]])
)
modified.set_offsets([[], []])
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that this should actually be column_stack and I should get rid of the transpose in the set method. Most usage will be with column_stack i suspect.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please check what other set_offsets methods do. We should aim for consistency.

np.testing.assert_allclose(expected.get_offsets(), modified.get_offsets())