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

Skip to content

Commit 89823a5

Browse files
committed
Fix linear segmented colormap with one element
1 parent d062859 commit 89823a5

2 files changed

Lines changed: 24 additions & 10 deletions

File tree

lib/matplotlib/colors.py

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -383,16 +383,20 @@ def makeMappingArray(N, data, gamma=1.0):
383383
if (np.diff(x) < 0).any():
384384
raise ValueError("data mapping points must have x in increasing order")
385385
# begin generation of lookup table
386-
x = x * (N - 1)
387-
xind = (N - 1) * np.linspace(0, 1, N) ** gamma
388-
ind = np.searchsorted(x, xind)[1:-1]
389-
390-
distance = (xind[1:-1] - x[ind - 1]) / (x[ind] - x[ind - 1])
391-
lut = np.concatenate([
392-
[y1[0]],
393-
distance * (y0[ind] - y1[ind - 1]) + y1[ind - 1],
394-
[y0[-1]],
395-
])
386+
if N == 1:
387+
# convention: use the y = f(x=1) value for a 1-element lookup table
388+
lut = np.array(y0[-1])
389+
else:
390+
x = x * (N - 1)
391+
xind = (N - 1) * np.linspace(0, 1, N) ** gamma
392+
ind = np.searchsorted(x, xind)[1:-1]
393+
394+
distance = (xind[1:-1] - x[ind - 1]) / (x[ind] - x[ind - 1])
395+
lut = np.concatenate([
396+
[y1[0]],
397+
distance * (y0[ind] - y1[ind - 1]) + y1[ind - 1],
398+
[y0[-1]],
399+
])
396400
# ensure that the lut is confined to values between 0 and 1 by clipping it
397401
return np.clip(lut, 0.0, 1.0)
398402

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)