diff --git a/lib/matplotlib/colors.py b/lib/matplotlib/colors.py index 5ad5df873583..73509990d41d 100644 --- a/lib/matplotlib/colors.py +++ b/lib/matplotlib/colors.py @@ -852,7 +852,8 @@ def _resample(self, lutsize): """ Return a new color map with *lutsize* entries. """ - return ListedColormap(self.name, self.colors, lutsize) + colors = self(np.linspace(0, 1, lutsize)) + return ListedColormap(colors, name=self.name) class Normalize(object): diff --git a/lib/matplotlib/tests/test_colors.py b/lib/matplotlib/tests/test_colors.py index e440f07c4867..0ac1bceff80a 100644 --- a/lib/matplotlib/tests/test_colors.py +++ b/lib/matplotlib/tests/test_colors.py @@ -18,6 +18,28 @@ cleanup, knownfailureif) +def test_resample(): + """ + Github issue #6025 pointed to incorrect ListedColormap._resample; + here we test the method for LinearSegmentedColormap as well. + """ + n = 101 + colorlist = np.empty((n, 4), float) + colorlist[:, 0] = np.linspace(0, 1, n) + colorlist[:, 1] = 0.2 + colorlist[:, 2] = np.linspace(1, 0, n) + colorlist[:, 3] = 0.7 + lsc = mcolors.LinearSegmentedColormap.from_list('lsc', colorlist) + lc = mcolors.ListedColormap(colorlist) + 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) + + def test_colormap_endian(): """ Github issue #1005: a bug in putmask caused erroneous