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 16233e0

Browse filesBrowse files
committed
BUG: fix ListedColormap._resample, hence plt.get_cmap; closes #6025
1 parent c0728d2 commit 16233e0
Copy full SHA for 16233e0

File tree

Expand file treeCollapse file tree

2 files changed

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

2 files changed

+23
-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+
vals = self(np.linspace(0, 1, lutsize))
856+
return ListedColormap(vals, 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
+21Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,27 @@
1717
from matplotlib.testing.decorators import (image_comparison,
1818
cleanup, knownfailureif)
1919

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

2142
def test_colormap_endian():
2243
"""

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.