Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 25ff2ed

Browse files
jklymakmeeseeksmachine
authored andcommitted
Backport PR #21818: Fix collections coerce float
1 parent be3b9bb commit 25ff2ed

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

lib/matplotlib/collections.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -559,10 +559,12 @@ def set_offsets(self, offsets):
559559
----------
560560
offsets : (N, 2) or (2,) array-like
561561
"""
562-
offsets = np.asanyarray(offsets, float)
562+
offsets = np.asanyarray(offsets)
563563
if offsets.shape == (2,): # Broadcast (2,) -> (1, 2) but nothing else.
564564
offsets = offsets[None, :]
565-
self._offsets = offsets
565+
self._offsets = np.column_stack(
566+
(np.asarray(self.convert_xunits(offsets[:, 0]), 'float'),
567+
np.asarray(self.convert_yunits(offsets[:, 1]), 'float')))
566568
self.stale = True
567569

568570
def get_offsets(self):

lib/matplotlib/tests/test_collections.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1084,3 +1084,23 @@ def test_set_offset_transform():
10841084
late.set_offset_transform(skew)
10851085

10861086
assert skew == init.get_offset_transform() == late.get_offset_transform()
1087+
1088+
1089+
def test_set_offset_units():
1090+
# passing the offsets in initially (i.e. via scatter)
1091+
# should yield the same results as `set_offsets`
1092+
x = np.linspace(0, 10, 5)
1093+
y = np.sin(x)
1094+
d = x * np.timedelta64(24, 'h') + np.datetime64('2021-11-29')
1095+
1096+
sc = plt.scatter(d, y)
1097+
off0 = sc.get_offsets()
1098+
sc.set_offsets(list(zip(d, y)))
1099+
np.testing.assert_allclose(off0, sc.get_offsets())
1100+
1101+
# try the other way around
1102+
fig, ax = plt.subplots()
1103+
sc = ax.scatter(y, d)
1104+
off0 = sc.get_offsets()
1105+
sc.set_offsets(list(zip(y, d)))
1106+
np.testing.assert_allclose(off0, sc.get_offsets())

0 commit comments

Comments
 (0)