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

Skip to content

BUG: fix ListedColormap._resample, hence plt.get_cmap; closes #6025 #6036

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
Feb 21, 2016
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
3 changes: 2 additions & 1 deletion lib/matplotlib/colors.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
22 changes: 22 additions & 0 deletions lib/matplotlib/tests/test_colors.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down