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

Skip to content

Making sure to keep over/under/bad in cmap resample/reverse. #16937

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 24 additions & 4 deletions lib/matplotlib/colors.py
Original file line number Diff line number Diff line change
Expand Up @@ -790,7 +790,12 @@ def from_list(name, colors, N=256, gamma=1.0):

def _resample(self, lutsize):
"""Return a new color map with *lutsize* entries."""
return LinearSegmentedColormap(self.name, self._segmentdata, lutsize)
new_cmap = LinearSegmentedColormap(self.name, self._segmentdata,
lutsize)
new_cmap._rgba_over = self._rgba_over
new_cmap._rgba_under = self._rgba_under
new_cmap._rgba_bad = self._rgba_bad
return new_cmap

# Helper ensuring picklability of the reversed cmap.
@staticmethod
Expand Down Expand Up @@ -821,7 +826,12 @@ def reversed(self, name=None):
[(1.0 - x, y1, y0) for x, y0, y1 in reversed(data)])
for key, data in self._segmentdata.items()}

return LinearSegmentedColormap(name, data_r, self.N, self._gamma)
new_cmap = LinearSegmentedColormap(name, data_r, self.N, self._gamma)
# Reverse the over/under values too
new_cmap._rgba_over = self._rgba_under
new_cmap._rgba_under = self._rgba_over
new_cmap._rgba_bad = self._rgba_bad
return new_cmap


class ListedColormap(Colormap):
Expand Down Expand Up @@ -885,7 +895,12 @@ def _init(self):
def _resample(self, lutsize):
"""Return a new color map with *lutsize* entries."""
colors = self(np.linspace(0, 1, lutsize))
return ListedColormap(colors, name=self.name)
new_cmap = ListedColormap(colors, name=self.name)
# Keep the over/under values too
new_cmap._rgba_over = self._rgba_over
new_cmap._rgba_under = self._rgba_under
new_cmap._rgba_bad = self._rgba_bad
return new_cmap

def reversed(self, name=None):
"""
Expand All @@ -906,7 +921,12 @@ def reversed(self, name=None):
name = self.name + "_r"

colors_r = list(reversed(self.colors))
return ListedColormap(colors_r, name=name, N=self.N)
new_cmap = ListedColormap(colors_r, name=name, N=self.N)
# Reverse the over/under values too
new_cmap._rgba_over = self._rgba_under
new_cmap._rgba_under = self._rgba_over
new_cmap._rgba_bad = self._rgba_bad
return new_cmap


class Normalize:
Expand Down
16 changes: 16 additions & 0 deletions lib/matplotlib/tests/test_colors.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,25 @@ def test_resample():
colorlist[:, 3] = 0.7
lsc = mcolors.LinearSegmentedColormap.from_list('lsc', colorlist)
lc = mcolors.ListedColormap(colorlist)
# Set some bad values for testing too
for cmap in [lsc, lc]:
cmap.set_under('r')
cmap.set_over('g')
cmap.set_bad('b')
lsc3 = lsc._resample(3)
lc3 = lc._resample(3)
expected = np.array([[0.0, 0.2, 1.0, 0.7],
[0.5, 0.2, 0.5, 0.7],
[1.0, 0.2, 0.0, 0.7]], float)
assert_array_almost_equal(lsc3([0, 0.5, 1]), expected)
assert_array_almost_equal(lc3([0, 0.5, 1]), expected)
# Test over/under was copied properly
assert_array_almost_equal(lsc(np.inf), lsc3(np.inf))
assert_array_almost_equal(lsc(-np.inf), lsc3(-np.inf))
assert_array_almost_equal(lsc(np.nan), lsc3(np.nan))
assert_array_almost_equal(lc(np.inf), lc3(np.inf))
assert_array_almost_equal(lc(-np.inf), lc3(-np.inf))
assert_array_almost_equal(lc(np.nan), lc3(np.nan))


def test_register_cmap():
Expand Down Expand Up @@ -818,6 +830,10 @@ def test_colormap_reversing(name):
cmap._init()
cmap_r._init()
assert_array_almost_equal(cmap._lut[:-3], cmap_r._lut[-4::-1])
# Test the bad, over, under values too
assert_array_almost_equal(cmap(-np.inf), cmap_r(np.inf))
assert_array_almost_equal(cmap(np.inf), cmap_r(-np.inf))
assert_array_almost_equal(cmap(np.nan), cmap_r(np.nan))


def test_cn():
Expand Down