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

Skip to content

Commit 48eef46

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 48eef46

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

lib/matplotlib/colors.py

Lines changed: 2 additions & 2 deletions
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

Lines changed: 28 additions & 0 deletions
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):

0 commit comments

Comments
 (0)