diff --git a/branca/colormap.py b/branca/colormap.py index 2e76ba5..daeb9d9 100644 --- a/branca/colormap.py +++ b/branca/colormap.py @@ -586,11 +586,15 @@ def to_linear( Maximum number of legend tick labels """ + if index is None and len(self.colors) == 1: + index = [self.vmin, self.vmax] if index is None: - n = len(self.index) - 1 + bounds = self.index + if len(bounds) == len(self.colors): + bounds = [*bounds, max(self.vmax, bounds[-1])] + n = len(bounds) - 1 index = [ - self.index[i] * (1.0 - i / (n - 1.0)) - + self.index[i + 1] * i / (n - 1.0) + bounds[i] * (1.0 - i / (n - 1.0)) + bounds[i + 1] * i / (n - 1.0) for i in range(n) ] diff --git a/tests/test_colormap.py b/tests/test_colormap.py index cd30405..ffffc8c 100644 --- a/tests/test_colormap.py +++ b/tests/test_colormap.py @@ -100,6 +100,17 @@ def test_step_to_linear(): step.to_linear() +@pytest.mark.parametrize("colors", [["red"], ["red", "blue"], ["red", "lime", "blue"]]) +def test_step_to_linear_with_lower_bounds(colors): + lower_bounds = [0, 1, 4][: len(colors)] + step = cm.StepColormap(colors, index=lower_bounds, vmax=10) + explicit = cm.StepColormap(colors, index=[*lower_bounds, 10], vmax=10) + linear = step.to_linear() + assert linear.index == explicit.to_linear().index + assert linear.colors == (step.colors if len(colors) > 1 else step.colors * 2) + assert step.index == lower_bounds + + def test_linear_object(): cm.linear.OrRd_06._repr_html_() cm.linear.PuBu_06.to_step(12)