From 585a0009406d3e390392ea6b11d8746530c1056f Mon Sep 17 00:00:00 2001 From: Jody Klymak Date: Tue, 30 Nov 2021 16:30:59 +0100 Subject: [PATCH 1/2] FIX: collections set_offset co-ercing to float --- lib/matplotlib/collections.py | 5 ++++- lib/matplotlib/tests/test_collections.py | 20 ++++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/lib/matplotlib/collections.py b/lib/matplotlib/collections.py index 47f2c9503be7..a31afb86f0c6 100644 --- a/lib/matplotlib/collections.py +++ b/lib/matplotlib/collections.py @@ -545,9 +545,12 @@ def set_offsets(self, offsets): ---------- offsets : (N, 2) or (2,) array-like """ - offsets = np.asanyarray(offsets, float) + offsets = np.asanyarray(offsets) if offsets.shape == (2,): # Broadcast (2,) -> (1, 2) but nothing else. offsets = offsets[None, :] + offsets[:, 0] = self.convert_xunits(offsets[:, 0]) + offsets[:, 1] = self.convert_yunits(offsets[:, 1]) + offsets = np.asarray(offsets, 'float') self._offsets = offsets self.stale = True diff --git a/lib/matplotlib/tests/test_collections.py b/lib/matplotlib/tests/test_collections.py index e528fdc86be3..db458e4b5c5f 100644 --- a/lib/matplotlib/tests/test_collections.py +++ b/lib/matplotlib/tests/test_collections.py @@ -1078,3 +1078,23 @@ def test_set_offset_transform(): late.set_offset_transform(skew) assert skew == init.get_offset_transform() == late.get_offset_transform() + + +def test_set_offset_units(): + # passing the offsets in initially (i.e. via scatter) + # should yield the same results as `set_offsets` + x = np.linspace(0, 10, 5) + y = np.sin(x) + d = x * np.timedelta64(24, 'h') + np.datetime64('2021-11-29') + + sc = plt.scatter(d, y) + off0 = sc.get_offsets() + sc.set_offsets(list(zip(d, y))) + np.testing.assert_allclose(off0, sc.get_offsets()) + + # try the other way around + fig, ax = plt.subplots() + sc = ax.scatter(y, d) + off0 = sc.get_offsets() + sc.set_offsets(list(zip(y, d))) + np.testing.assert_allclose(off0, sc.get_offsets()) From 4d860b17991643cea63bf6849c094bef30119c99 Mon Sep 17 00:00:00 2001 From: Jody Klymak Date: Thu, 2 Dec 2021 08:53:40 +0100 Subject: [PATCH 2/2] FIX: --- lib/matplotlib/collections.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/lib/matplotlib/collections.py b/lib/matplotlib/collections.py index a31afb86f0c6..5c242b9086c1 100644 --- a/lib/matplotlib/collections.py +++ b/lib/matplotlib/collections.py @@ -548,10 +548,9 @@ def set_offsets(self, offsets): offsets = np.asanyarray(offsets) if offsets.shape == (2,): # Broadcast (2,) -> (1, 2) but nothing else. offsets = offsets[None, :] - offsets[:, 0] = self.convert_xunits(offsets[:, 0]) - offsets[:, 1] = self.convert_yunits(offsets[:, 1]) - offsets = np.asarray(offsets, 'float') - self._offsets = offsets + 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):