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

Skip to content

Fix collections coerce float #21818

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

Merged
merged 2 commits into from
Dec 3, 2021
Merged
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
6 changes: 4 additions & 2 deletions lib/matplotlib/collections.py
Original file line number Diff line number Diff line change
Expand Up @@ -545,10 +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, :]
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):
Expand Down
20 changes: 20 additions & 0 deletions lib/matplotlib/tests/test_collections.py
Original file line number Diff line number Diff line change
Expand Up @@ -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())