Skip to content

Commit 33beff9

Browse files
authored
Merge pull request #20511 from QuLogic/fix-ma.masked_where
Fix calls to np.ma.masked_where
2 parents 1f204c0 + 48eef46 commit 33beff9

File tree

4 files changed

+46
-5
lines changed

4 files changed

+46
-5
lines changed

lib/matplotlib/colors.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1545,11 +1545,11 @@ class LogNorm(Normalize):
15451545

15461546
def autoscale(self, A):
15471547
# docstring inherited.
1548-
super().autoscale(np.ma.masked_less_equal(A, 0, copy=False))
1548+
super().autoscale(np.ma.array(A, mask=(A <= 0)))
15491549

15501550
def autoscale_None(self, A):
15511551
# docstring inherited.
1552-
super().autoscale_None(np.ma.masked_less_equal(A, 0, copy=False))
1552+
super().autoscale_None(np.ma.array(A, mask=(A <= 0)))
15531553

15541554

15551555
@_make_norm_from_scale(

lib/matplotlib/quiver.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -1158,8 +1158,10 @@ def _make_barbs(self, u, v, nflags, nbarbs, half_barb, empty_flag, length,
11581158
return barb_list
11591159

11601160
def set_UVC(self, U, V, C=None):
1161-
self.u = ma.masked_invalid(U, copy=False).ravel()
1162-
self.v = ma.masked_invalid(V, copy=False).ravel()
1161+
# We need to ensure we have a copy, not a reference to an array that
1162+
# might change before draw().
1163+
self.u = ma.masked_invalid(U, copy=True).ravel()
1164+
self.v = ma.masked_invalid(V, copy=True).ravel()
11631165

11641166
# Flip needs to have the same number of entries as everything else.
11651167
# Use broadcast_to to avoid a bloated array of identical values.
@@ -1170,7 +1172,7 @@ def set_UVC(self, U, V, C=None):
11701172
flip = self.flip
11711173

11721174
if C is not None:
1173-
c = ma.masked_invalid(C, copy=False).ravel()
1175+
c = ma.masked_invalid(C, copy=True).ravel()
11741176
x, y, u, v, c, flip = cbook.delete_masked_points(
11751177
self.x.ravel(), self.y.ravel(), self.u, self.v, c,
11761178
flip.ravel())

lib/matplotlib/tests/test_image.py

+28
Original file line numberDiff line numberDiff line change
@@ -1233,6 +1233,34 @@ def test_imshow_quantitynd():
12331233
fig.canvas.draw()
12341234

12351235

1236+
@check_figures_equal(extensions=['png'])
1237+
def test_norm_change(fig_test, fig_ref):
1238+
# LogNorm should not mask anything invalid permanently.
1239+
data = np.full((5, 5), 1, dtype=np.float64)
1240+
data[0:2, :] = -1
1241+
1242+
masked_data = np.ma.array(data, mask=False)
1243+
masked_data.mask[0:2, 0:2] = True
1244+
1245+
cmap = plt.get_cmap('viridis').with_extremes(under='w')
1246+
1247+
ax = fig_test.subplots()
1248+
im = ax.imshow(data, norm=colors.LogNorm(vmin=0.5, vmax=1),
1249+
extent=(0, 5, 0, 5), interpolation='nearest', cmap=cmap)
1250+
im.set_norm(colors.Normalize(vmin=-2, vmax=2))
1251+
im = ax.imshow(masked_data, norm=colors.LogNorm(vmin=0.5, vmax=1),
1252+
extent=(5, 10, 5, 10), interpolation='nearest', cmap=cmap)
1253+
im.set_norm(colors.Normalize(vmin=-2, vmax=2))
1254+
ax.set(xlim=(0, 10), ylim=(0, 10))
1255+
1256+
ax = fig_ref.subplots()
1257+
ax.imshow(data, norm=colors.Normalize(vmin=-2, vmax=2),
1258+
extent=(0, 5, 0, 5), interpolation='nearest', cmap=cmap)
1259+
ax.imshow(masked_data, norm=colors.Normalize(vmin=-2, vmax=2),
1260+
extent=(5, 10, 5, 10), interpolation='nearest', cmap=cmap)
1261+
ax.set(xlim=(0, 10), ylim=(0, 10))
1262+
1263+
12361264
@pytest.mark.parametrize('x', [-1, 1])
12371265
@check_figures_equal(extensions=['png'])
12381266
def test_huge_range_log(fig_test, fig_ref, x):

lib/matplotlib/tests/test_quiver.py

+11
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,17 @@ def test_barbs_flip():
202202
flip_barb=Y < 0)
203203

204204

205+
def test_barb_copy():
206+
fig, ax = plt.subplots()
207+
u = np.array([1.1])
208+
v = np.array([2.2])
209+
b0 = ax.barbs([1], [1], u, v)
210+
u[0] = 0
211+
assert b0.u[0] == 1.1
212+
v[0] = 0
213+
assert b0.v[0] == 2.2
214+
215+
205216
def test_bad_masked_sizes():
206217
"""Test error handling when given differing sized masked arrays."""
207218
x = np.arange(3)

0 commit comments

Comments
 (0)