Skip to content

Commit bf73d48

Browse files
committed
Use np.ma functions only when input is masked
1 parent eaf830f commit bf73d48

File tree

2 files changed

+39
-6
lines changed

2 files changed

+39
-6
lines changed

lib/matplotlib/collections.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -545,9 +545,14 @@ def set_offsets(self, offsets):
545545
offsets = np.asanyarray(offsets)
546546
if offsets.shape == (2,): # Broadcast (2,) -> (1, 2) but nothing else.
547547
offsets = offsets[None, :]
548-
self._offsets = np.ma.column_stack(
549-
(np.asanyarray(self.convert_xunits(offsets[:, 0]), float),
550-
np.asanyarray(self.convert_yunits(offsets[:, 1]), float)))
548+
if isinstance(offsets, np.ma.MaskedArray):
549+
self._offsets = np.ma.column_stack(
550+
(np.asanyarray(self.convert_xunits(offsets[:, 0]), float),
551+
np.asanyarray(self.convert_yunits(offsets[:, 1]), float)))
552+
else:
553+
self._offsets = np.column_stack(
554+
(np.asanyarray(self.convert_xunits(offsets[:, 0]), float),
555+
np.asanyarray(self.convert_yunits(offsets[:, 1]), float)))
551556
self.stale = True
552557

553558
def get_offsets(self):

lib/matplotlib/tests/test_collections.py

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1151,12 +1151,40 @@ def test_check_masked_offsets():
11511151
ax.scatter(unmasked_x, masked_y)
11521152

11531153

1154-
def test_masked_set_offsets():
1154+
@check_figures_equal(extensions=["png"])
1155+
def test_masked_set_offsets(fig_ref, fig_test):
1156+
x = np.ma.array([1, 2, 3, 4, 5], mask=[0, 0, 1, 1, 0])
1157+
y = np.arange(1, 6)
1158+
1159+
ax_test = fig_test.add_subplot()
1160+
scat = ax_test.scatter(x, y)
1161+
x += 1
1162+
scat.set_offsets(np.ma.column_stack([x, y]))
1163+
ax_test.set_xticks([])
1164+
ax_test.set_yticks([])
1165+
ax_test.set_xlim(0, 7)
1166+
ax_test.set_ylim(0, 6)
1167+
1168+
ax_ref = fig_ref.add_subplot()
1169+
ax_ref.scatter([2, 3, 6], [1, 2, 5])
1170+
ax_ref.set_xticks([])
1171+
ax_ref.set_yticks([])
1172+
ax_ref.set_xlim(0, 7)
1173+
ax_ref.set_ylim(0, 6)
1174+
1175+
1176+
def test_check_offsets_dtype():
1177+
# Check that setting offsets doesn't change dtype
11551178
x = np.ma.array([1, 2, 3, 4, 5], mask=[0, 0, 1, 1, 0])
11561179
y = np.arange(1, 6)
11571180

11581181
fig, ax = plt.subplots()
11591182
scat = ax.scatter(x, y)
11601183
x += 1
1161-
scat.set_offsets(np.ma.column_stack([x, y]))
1162-
assert np.ma.is_masked(scat.get_offsets())
1184+
masked_offsets = np.ma.column_stack([x, y])
1185+
scat.set_offsets(masked_offsets)
1186+
assert isinstance(scat.get_offsets(), type(masked_offsets))
1187+
1188+
unmasked_offsets = np.column_stack([x, y])
1189+
scat.set_offsets(unmasked_offsets)
1190+
assert isinstance(scat.get_offsets(), type(unmasked_offsets))

0 commit comments

Comments
 (0)