8000 Merge pull request #14241 from timhoffm/fix-makeMappingArray · matplotlib/matplotlib@40791c9 · GitHub
[go: up one dir, main page]

Skip to content

Commit 40791c9

Browse files
authored
Merge pull request #14241 from timhoffm/fix-makeMappingArray
Fix linear segmented colormap with one element
2 parents 4a9b1c1 + 89823a5 commit 40791c9

File tree

2 files changed

+24
-10
lines changed

2 files changed

+24
-10
lines changed

lib/matplotlib/colors.py

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -419,16 +419,20 @@ def makeMappingArray(N, data, gamma=1.0):
419419
if (np.diff(x) < 0).any():
420420
raise ValueError("data mapping points must have x in increasing order")
421421
# begin generation of lookup table
422-
x = x * (N - 1)
423-
xind = (N - 1) * np.linspace(0, 1, N) ** gamma
424-
ind = np.searchsorted(x, xind)[1:-1]
425-
426-
distance = (xind[1:-1] - x[ind - 1]) / (x[ind] - x[ind - 1])
427-
lut = np.concatenate([
428-
[y1[0]],
429-
distance * (y0[ind] - y1[ind - 1]) + y1[ind - 1],
430-
[y0[-1]],
431-
])
422+
if N == 1:
423+
# convention: use the y = f(x=1) value for a 1-element lookup table
424+
lut = np.array(y0[-1])
425+
else:
426+
x = x * (N - 1)
427+
xind = (N - 1) * np.linspace(0, 1, N) ** gamma
428+
ind = np.searchsorted(x, xind)[1:-1]
429+
430+
distance = (xind[1:-1] - x[ind - 1]) / (x[ind] - x[ind - 1])
431+
lut = np.concatenate([
432+
[y1[0]],
433+
distance * (y0[ind] - y1[ind - 1]) + y1[ind - 1],
434+
[y0[-1]],
435+
])
432436
# ensure that the lut is confined to values between 0 and 1 by clipping it
433437
return np.clip(lut, 0.0, 1.0)
434438

lib/matplotlib/tests/test_colors.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,16 @@
1616
from matplotlib.testing.decorators import image_comparison
1717

1818

19+
@pytest.mark.parametrize('N, result', [
20+
(5, [1, .6, .2, .1, 0]),
21+
(2, [1, 0]),
22+
(1, [0]),
23+
])
24+
def test_makeMappingArray(N, result):
25+
data = [(0.0, 1.0, 1.0), (0.5, 0.2, 0.2), (1.0, 0.0, 0.0)]
26+
assert_array_almost_equal(mcolors.makeMappingArray(N, data), result)
27+
28+
1929
def test_resample():
2030
"""
2131
Github issue #6025 pointed to incorrect ListedColormap._resample;

0 commit comments

Comments
 (0)
0