From e56982ede7568f013f40ad56fa8aad67e1ec5d5c Mon Sep 17 00:00:00 2001 From: Ian Hunt-Isaak Date: Sat, 5 Feb 2022 14:06:30 -0500 Subject: [PATCH] allow setting collection offsets to empty --- lib/matplotlib/collections.py | 12 ++++++++---- lib/matplotlib/tests/test_collections.py | 10 ++++++++++ 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/lib/matplotlib/collections.py b/lib/matplotlib/collections.py index a7c8b631c26d..8a7b5c181325 100644 --- a/lib/matplotlib/collections.py +++ b/lib/matplotlib/collections.py @@ -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): + 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): diff --git a/lib/matplotlib/tests/test_collections.py b/lib/matplotlib/tests/test_collections.py index 488d5a34fd81..ab24edbe77d2 100644 --- a/lib/matplotlib/tests/test_collections.py +++ b/lib/matplotlib/tests/test_collections.py @@ -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([[], []]) + np.testing.assert_allclose(expected.get_offsets(), modified.get_offsets())