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

Appearance settings

Commit 9dca4d8

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

File tree

Expand file treeCollapse file tree

2 files changed

+24
-1
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+24
-1
lines changed

‎lib/matplotlib/colors.py

Copy file name to clipboardExpand all lines: lib/matplotlib/colors.py
+2-1Lines changed: 2 additions & 1 deletion
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

Copy file name to clipboardExpand all lines: lib/matplotlib/tests/test_colors.py
+22Lines changed: 22 additions & 0 deletions
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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.