Skip to content

Commit 076c4e6

Browse files
committed
Don't modify arrays when masking values for log.
NumPy 1.21.0 fixed a bug with `np.ma.masked_where` where `copy=False` now modifies the input if it is masked, which we do not want to do. `np.ma.array` defaults to not copying the data, but copies the mask (adding the new mask), which is what we wanted with `copy=False`.
1 parent b3bf734 commit 076c4e6

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
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/tests/test_image.py

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

12351235

1236+
def test_norm_change(fig_test, fig_ref):
1237+
# LogNorm should not mask anything invalid permanently.
1238+
data = np.full((5, 5), 1, dtype=np.float64)
1239+
data[0:2, :] = -1
1240+
1241+
cmap = plt.get_cmap('viridis').with_extremes(under='w')
1242+
1243+
ax = fig_test.subplots()
1244+
im = ax.imshow(data, norm=colors.LogNorm(vmin=0.5, vmax=1),
1245+
interpolation='nearest', cmap=cmap)
1246+
im.set_norm(colors.Normalize(vmin=-2, vmax=2))
1247+
1248+
ax = fig_ref.subplots()
1249+
im = ax.imshow(data, norm=colors.Normalize(vmin=-2, vmax=2),
1250+
interpolation='nearest', cmap=cmap)
1251+
1252+
12361253
@pytest.mark.parametrize('x', [-1, 1])
12371254
@check_figures_equal(extensions=['png'])
12381255
def test_huge_range_log(fig_test, fig_ref, x):

0 commit comments

Comments
 (0)