Skip to content

Commit a899811

Browse files
committed
FIX: return static view of good/bad/under
Otherwise we will hand a view back to the user and it will change under them if `set_under` (e.g.) is called.
1 parent 5b835ee commit a899811

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

lib/matplotlib/colors.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -652,7 +652,7 @@ def get_bad(self):
652652
"""Get the color for masked values."""
653653
if not self._isinit:
654654
self._init()
655-
return self._lut[self._i_bad]
655+
return np.array(self._lut[self._i_bad])
656656

657657
def set_bad(self, color='k', alpha=None):
658658
"""Set the color for masked values."""
@@ -665,7 +665,7 @@ def get_under(self):
665665
"""Get the color for low out-of-range values."""
666666
if not self._isinit:
667667
self._init()
668-
return self._lut[self._i_under]
668+
return np.array(self._lut[self._i_under])
669669

670670
def set_under(self, color='k', alpha=None):
671671
"""Set the color for low out-of-range values."""
@@ -678,7 +678,7 @@ def get_over(self):
678678
"""Get the color for high out-of-range values."""
679679
if not self._isinit:
680680
self._init()
681-
return self._lut[self._i_over]
681+
return np.array(self._lut[self._i_over])
682682

683683
def set_over(self, color='k', alpha=None):
684684
"""Set the color for high out-of-range values."""

lib/matplotlib/tests/test_colors.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1187,6 +1187,16 @@ def test_get_under_over_bad():
11871187
assert_array_equal(cmap.get_bad(), cmap(np.nan))
11881188

11891189

1190+
@pytest.mark.parametrize('kind', ('over', 'under', 'bad'))
1191+
def test_non_mutable_get_values(kind):
1192+
cmap = copy.copy(plt.get_cmap('viridis'))
1193+
init_value = getattr(cmap, f'get_{kind}')()
1194+
getattr(cmap, f'set_{kind}')('k')
1195+
black_value = getattr(cmap, f'get_{kind}')()
1196+
assert np.all(black_value == [0, 0, 0, 1])
1197+
assert not np.all(init_value == black_value)
1198+
1199+
11901200
def test_colormap_alpha_array():
11911201
cmap = plt.get_cmap('viridis')
11921202
vals = [-1, 0.5, 2] # under, valid, over

0 commit comments

Comments
 (0)