Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit d733f9f

Browse filesBrowse 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 d733f9f
Copy full SHA for d733f9f

File tree

2 files changed

+28
-2
lines changed
Filter options

2 files changed

+28
-2
lines changed

‎lib/matplotlib/colors.py

Copy file name to clipboardExpand all lines: lib/matplotlib/colors.py
+2-2Lines 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

Copy file name to clipboardExpand all lines: lib/matplotlib/tests/test_image.py
+26Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1233,6 +1233,32 @@ 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+
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+
interpolation='nearest', cmap=cmap)
1253+
im.set_norm(colors.Normalize(vmin=-2, vmax=2))
1254+
1255+
ax = fig_ref.subplots()
1256+
ax.imshow(data, norm=colors.Normalize(vmin=-2, vmax=2),
1257+
interpolation='nearest', cmap=cmap)
1258+
ax.imshow(masked_data, norm=colors.Normalize(vmin=-2, vmax=2),
1259+
interpolation='nearest', cmap=cmap)
1260+
1261+
12361262
@pytest.mark.parametrize('x', [-1, 1])
12371263
@check_figures_equal(extensions=['png'])
12381264
def test_huge_range_log(fig_test, fig_ref, x):

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.