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

Skip to content

Allow using masked in set_offsets #24757

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 5 commits into from
Dec 21, 2022
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
8 changes: 5 additions & 3 deletions lib/matplotlib/collections.py
Original file line number Diff line number Diff line change
Expand Up @@ -545,9 +545,11 @@ def set_offsets(self, offsets):
offsets = np.asanyarray(offsets)
if offsets.shape == (2,): # Broadcast (2,) -> (1, 2) but nothing else.
offsets = offsets[None, :]
self._offsets = np.column_stack(
(np.asarray(self.convert_xunits(offsets[:, 0]), float),
np.asarray(self.convert_yunits(offsets[:, 1]), float)))
cstack = (np.ma.column_stack if isinstance(offsets, np.ma.MaskedArray)
else np.column_stack)
self._offsets = cstack(
(np.asanyarray(self.convert_xunits(offsets[:, 0]), float),
np.asanyarray(self.convert_yunits(offsets[:, 1]), float)))
self.stale = True

def get_offsets(self):
Expand Down
33 changes: 33 additions & 0 deletions lib/matplotlib/tests/test_collections.py
Original file line number Diff line number Diff line change
Expand Up @@ -1149,3 +1149,36 @@ def test_check_masked_offsets():

fig, ax = plt.subplots()
ax.scatter(unmasked_x, masked_y)


@check_figures_equal(extensions=["png"])
def test_masked_set_offsets(fig_ref, fig_test):
x = np.ma.array([1, 2, 3, 4, 5], mask=[0, 0, 1, 1, 0])
y = np.arange(1, 6)

ax_test = fig_test.add_subplot()
scat = ax_test.scatter(x, y)
scat.set_offsets(np.ma.column_stack([x, y]))
ax_test.set_xticks([])
ax_test.set_yticks([])

ax_ref = fig_ref.add_subplot()
ax_ref.scatter([1, 2, 5], [1, 2, 5])
ax_ref.set_xticks([])
ax_ref.set_yticks([])


def test_check_offsets_dtype():
# Check that setting offsets doesn't change dtype
x = np.ma.array([1, 2, 3, 4, 5], mask=[0, 0, 1, 1, 0])
y = np.arange(1, 6)

fig, ax = plt.subplots()
scat = ax.scatter(x, y)
masked_offsets = np.ma.column_stack([x, y])
scat.set_offsets(masked_offsets)
assert isinstance(scat.get_offsets(), type(masked_offsets))

unmasked_offsets = np.column_stack([x, y])
scat.set_offsets(unmasked_offsets)
assert isinstance(scat.get_offsets(), type(unmasked_offsets))