Movatterモバイル変換


[0]ホーム

URL:


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

Sign up
Appearance settings

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes fromall 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 deletionslib/matplotlib/colors.py
View file
Open in desktop
Original file line numberDiff line numberDiff 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 DownExpand 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 DownExpand 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 deletionslib/matplotlib/tests/test_colors.py
View file
Open in desktop
Original file line numberDiff line numberDiff 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 DownExpand 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

[8]ページ先頭

©2009-2025 Movatter.jp