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

Skip to content

Commit 392dd40

Browse files
authored
Fix collections coerce float (#21818)
* FIX: collections set_offset co-ercing to float
1 parent 9d73164 commit 392dd40

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
@@ -545,10 +545,12 @@ def set_offsets(self, offsets):
545545
----------
546546
offsets : (N, 2) or (2,) array-like
547547
"""
548-
offsets = np.asanyarray(offsets, float)
548+
offsets = np.asanyarray(offsets)
549549
if offsets.shape == (2,): # Broadcast (2,) -> (1, 2) but nothing else.
550550
offsets = offsets[None, :]
551-
self._offsets = offsets
551+
self._offsets = np.column_stack(
552+
(np.asarray(self.convert_xunits(offsets[:, 0]), 'float'),
553+
np.asarray(self.convert_yunits(offsets[:, 1]), 'float')))
552554
self.stale = True
553555

554556
def get_offsets(self):

lib/matplotlib/tests/test_collections.py

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

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

0 commit comments

Comments
 (0)