8000 Merge pull request #14251 from meeseeksmachine/auto-backport-of-pr-14… · matplotlib/matplotlib@6ff5082 · GitHub
[go: up one dir, main page]

Skip to content

Commit 6ff5082

Browse files
authored
Merge pull request #14251 from meeseeksmachine/auto-backport-of-pr-14241-on-v3.1.x
Backport PR #14241 on branch v3.1.x (Fix linear segmented colormap with one element)
2 parents 3b26ee6 + d4de838 commit 6ff5082

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
@@ -377,16 +377,20 @@ def makeMappingArray(N, data, gamma=1.0):
377377
if (np.diff(x) < 0).any():
378378
raise ValueError("data mapping points must have x in increasing order")
379379
# begin generation of lookup table
380-
x = x * (N - 1)
381-
xind = (N - 1) * np.linspace(0, 1, N) ** gamma
382-
ind = np.searchsorted(x, xind)[1:-1]
383-
384-
distance = (xind[1:-1] - x[ind - 1]) / (x[ind] - x[ind - 1])
385-
lut = np.concatenate([
386-
[y1[0]],
387-
distance * (y0[ind] - y1[ind - 1]) + y1[ind - 1],
388-
[y0[-1]],
389-
])
380+
if N == 1:
381+
# convention: use the y = f(x=1) value for a 1-element lookup table
382+
lut = np.array(y0[-1])
383+
else:
384+
x = x * (N - 1)
385+
xind = (N - 1) * np.linspace(0, 1, N) ** gamma
386+
ind = np.searchsorted(x, xind)[1:-1]
387+
388+
distance = (xind[1:-1] - x[ind - 1]) / (x[ind] - x[ind - 1])
389+
lut = np.concatenate([
390+
[y1[0]],
391+
distance * (y0[ind] - y1[ind - 1]) + y1[ind - 1],
392+
[y0[-1]],
393+
])
390394
# ensure that the lut is confined to values between 0 and 1 by clipping it
391395
return np.clip(lut, 0.0, 1.0)
392396

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