Skip to content

Commit 9dca4d8

Browse files
committed
Merge pull request #6036 from efiring/ListedColormap_resample
BUG: fix ListedColormap._resample, hence plt.get_cmap; closes #6025
1 parent e4c229f commit 9dca4d8

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

lib/matplotlib/colors.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -852,7 +852,8 @@ def _resample(self, lutsize):
852852
"""
853853
Return a new color map with *lutsize* entries.
854854
"""
855-
return ListedColormap(self.name, self.colors, lutsize)
855+
colors = self(np.linspace(0, 1, lutsize))
856+
return ListedColormap(colors, name=self.name)
856857

857858

858859
class Normalize(object):

lib/matplotlib/tests/test_colors.py

+22
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,28 @@
1818
cleanup, knownfailureif)
1919

2020

21+
def test_resample():
22+
"""
23+
Github issue #6025 pointed to incorrect ListedColormap._resample;
24+
here we test the method for LinearSegmentedColormap as well.
25+
"""
26+
n = 101
27+
colorlist = np.empty((n, 4), float)
28+
colorlist[:, 0] = np.linspace(0, 1, n)
29+
colorlist[:, 1] = 0.2
30+
colorlist[:, 2] = np.linspace(1, 0, n)
31+
colorlist[:, 3] = 0.7
32+
lsc = mcolors.LinearSegmentedColormap.from_list('lsc', colorlist)
33+
lc = mcolors.ListedColormap(colorlist)
34+
lsc3 = lsc._resample(3)
35+
lc3 = lc._resample(3)
36+
expected = np.array([[0.0, 0.2, 1.0, 0.7],
37+
[0.5, 0.2, 0.5, 0.7],
38+
[1.0, 0.2, 0.0, 0.7]], float)
39+
assert_array_almost_equal(lsc3([0, 0.5, 1]), expected)
40+
assert_array_almost_equal(lc3([0, 0.5, 1]), expected)
41+
42+
2143
def test_colormap_endian():
2244
"""
2345
Github issue #1005: a bug in putmask caused erroneous

0 commit comments

Comments
 (0)