Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions branca/colormap.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
]

Expand Down
11 changes: 11 additions & 0 deletions tests/test_colormap.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down