Skip to content

Commit c292827

Browse files
authored
Merge pull request #16937 from greglucas/colormap_reverse_extend
Making sure to keep over/under/bad in cmap resample/reverse.
2 parents f10bcee + 76aa2d5 commit c292827

File tree

2 files changed

+40
-4
lines changed

2 files changed

+40
-4
lines changed

lib/matplotlib/colors.py

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -790,7 +790,12 @@ def from_list(name, colors, N=256, gamma=1.0):
790790

791791
def _resample(self, lutsize):
792792
"""Return a new color map with *lutsize* entries."""
793-
return LinearSegmentedColormap(self.name, self._segmentdata, lutsize)
793+
new_cmap = LinearSegmentedColormap(self.name, self._segmentdata,
794+
lutsize)
795+
new_cmap._rgba_over = self._rgba_over
796+
new_cmap._rgba_under = self._rgba_under
797+
new_cmap._rgba_bad = self._rgba_bad
798+
return new_cmap
794799

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

824-
return LinearSegmentedColormap(name, data_r, self.N, self._gamma)
829+
new_cmap = LinearSegmentedColormap(name, data_r, self.N, self._gamma)
830+
# Reverse the over/under values too
831+
new_cmap._rgba_over = self._rgba_under
832+
new_cmap._rgba_under = self._rgba_over
833+
new_cmap._rgba_bad = self._rgba_bad
834+
return new_cmap
825835

826836

827837
class ListedColormap(Colormap):
@@ -885,7 +895,12 @@ def _init(self):
885895
def _resample(self, lutsize):
886896
"""Return a new color map with *lutsize* entries."""
887897
colors = self(np.linspace(0, 1, lutsize))
888-
return ListedColormap(colors, name=self.name)
898+
new_cmap = ListedColormap(colors, name=self.name)
899+
# Keep the over/under values too
900+
new_cmap._rgba_over = self._rgba_over
901+
new_cmap._rgba_under = self._rgba_under
902+
new_cmap._rgba_bad = self._rgba_bad
903+
return new_cmap
889904

890905
def reversed(self, name=None):
891906
"""
@@ -906,7 +921,12 @@ def reversed(self, name=None):
906921
name = self.name + "_r"
907922

908923
colors_r = list(reversed(self.colors))
909-
return ListedColormap(colors_r, name=name, N=self.N)
924+
new_cmap = ListedColormap(colors_r, name=name, N=self.N)
925+
# Reverse the over/under values too
926+
new_cmap._rgba_over = self._rgba_under
927+
new_cmap._rgba_under = self._rgba_over
928+
new_cmap._rgba_bad = self._rgba_bad
929+
return new_cmap
910930

911931

912932
class Normalize:

lib/matplotlib/tests/test_colors.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,25 @@ def test_resample():
3939
colorlist[:, 3] = 0.7
4040
lsc = mcolors.LinearSegmentedColormap.from_list('lsc', colorlist)
4141
lc = mcolors.ListedColormap(colorlist)
42+
# Set some bad values for testing too
43+
for cmap in [lsc, lc]:
44+
cmap.set_under('r')
45+
cmap.set_over('g')
46+
cmap.set_bad('b')
4247
lsc3 = lsc._resample(3)
4348
lc3 = lc._resample(3)
4449
expected = np.array([[0.0, 0.2, 1.0, 0.7],
4550
[0.5, 0.2, 0.5, 0.7],
4651
[1.0, 0.2, 0.0, 0.7]], float)
4752
assert_array_almost_equal(lsc3([0, 0.5, 1]), expected)
4853
assert_array_almost_equal(lc3([0, 0.5, 1]), expected)
54+
# Test over/under was copied properly
55+
assert_array_almost_equal(lsc(np.inf), lsc3(np.inf))
56+
assert_array_almost_equal(lsc(-np.inf), lsc3(-np.inf))
57+
assert_array_almost_equal(lsc(np.nan), lsc3(np.nan))
58+
assert_array_almost_equal(lc(np.inf), lc3(np.inf))
59+
assert_array_almost_equal(lc(-np.inf), lc3(-np.inf))
60+
assert_array_almost_equal(lc(np.nan), lc3(np.nan))
4961

5062

5163
def test_register_cmap():
@@ -818,6 +830,10 @@ def test_colormap_reversing(name):
818830
cmap._init()
819831
cmap_r._init()
820832
assert_array_almost_equal(cmap._lut[:-3], cmap_r._lut[-4::-1])
833+
# Test the bad, over, under values too
834+
assert_array_almost_equal(cmap(-np.inf), cmap_r(np.inf))
835+
assert_array_almost_equal(cmap(np.inf), cmap_r(-np.inf))
836+
assert_array_almost_equal(cmap(np.nan), cmap_r(np.nan))
821837

822838

823839
def test_cn():

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy