From 0ebdea5432f859fddbec6bb95c2486f9c9c5440a Mon Sep 17 00:00:00 2001 From: Jody Klymak Date: Wed, 24 Jun 2026 07:01:50 -0700 Subject: [PATCH] Backport PR #31943: FIX: allow non-strict monotonicity in LinearSegmented.from_list values --- lib/matplotlib/colors.py | 2 +- lib/matplotlib/tests/test_colors.py | 7 +++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/lib/matplotlib/colors.py b/lib/matplotlib/colors.py index 333b06464478..010f73131fbc 100644 --- a/lib/matplotlib/colors.py +++ b/lib/matplotlib/colors.py @@ -1233,7 +1233,7 @@ def from_list(name, colors, N=256, gamma=1.0, *, bad=None, under=None, over=None except Exception as e2: raise e2 from e vals = np.asarray(_vals) - if np.min(vals) < 0 or np.max(vals) > 1 or np.any(np.diff(vals) <= 0): + if np.min(vals) < 0 or np.max(vals) > 1 or np.any(np.diff(vals) < 0): raise ValueError( "the values passed in the (value, color) pairs " "must increase monotonically from 0 to 1." diff --git a/lib/matplotlib/tests/test_colors.py b/lib/matplotlib/tests/test_colors.py index 5bc1f8aea973..2ef9b7c5d091 100644 --- a/lib/matplotlib/tests/test_colors.py +++ b/lib/matplotlib/tests/test_colors.py @@ -1828,6 +1828,13 @@ def test_LinearSegmentedColormap_from_list_value_color_tuple(): ) +def test_LinearSegmentedColormap_from_list_repeat_values(): + # Smoke test that we can pass repeated values to make a solid band of color + # followed by a linear ramp. + value_color_tuples = [(0, "red"), (0.6, "red"), (0.6, "blue"), (1, "green")] + mcolors.LinearSegmentedColormap.from_list("lsc", value_color_tuples, N=11) + + @image_comparison(['test_norm_abc.png'], remove_text=True, style='_classic_test', tol=0 if platform.machine() == 'x86_64' else 0.05) def test_norm_abc():