Skip to content

Commit 95911d9

Browse files
authored
StepColormap: inclusive lower bound (#141)
1 parent a2fca5b commit 95911d9

2 files changed

Lines changed: 49 additions & 2 deletions

File tree

branca/colormap.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -457,7 +457,8 @@ class StepColormap(ColorMap):
457457
* HTML-like string (e.g: `"#ffff00`)
458458
* a color name or shortcut (e.g: `"y"` or `"yellow"`)
459459
index : list of floats, default None
460-
The values corresponding to each color.
460+
The bounds of the colors. The lower value is inclusive,
461+
the upper value is exclusive.
461462
It has to be sorted, and have the same length as `colors`.
462463
If None, a regular grid between `vmin` and `vmax` is created.
463464
vmin : float, default 0.
@@ -510,7 +511,7 @@ def rgba_floats_tuple(self, x):
510511
if x >= self.index[-1]:
511512
return self.colors[-1]
512513

513-
i = len([u for u in self.index if u < x]) # 0 < i < n.
514+
i = len([u for u in self.index if u <= x]) # 0 < i < n.
514515
return tuple(self.colors[i - 1])
515516

516517
def to_linear(self, index=None, max_labels=10):

tests/test_colormap.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,52 @@ def test_simple_linear():
2828
linear._repr_html_()
2929

3030

31+
black = "#000000ff"
32+
red = "#ff0000ff"
33+
green = "#00ff00ff"
34+
blue = "#0000ffff"
35+
36+
37+
def test_step_color_indexing():
38+
step = cm.StepColormap(colors=["black", "red", "lime", "blue"], index=[1, 2, 4, 5])
39+
assert step(0.99) == black
40+
assert step(1) == black
41+
assert step(1.01) == black
42+
assert step(1.99) == black
43+
assert step(2) == red
44+
assert step(2.01) == red
45+
assert step(3.99) == red
46+
assert step(4) == green
47+
assert step(4.01) == green
48+
assert step(4.99) == green
49+
assert step(5) == blue
50+
assert step(5.01) == blue
51+
52+
53+
def test_step_color_indexing_larger_index():
54+
# add an upper bound to the last color, which doesn't do much but shouldn't fail
55+
step = cm.StepColormap(
56+
colors=["black", "red", "lime", "blue"],
57+
index=[1, 2, 4, 5, 10],
58+
)
59+
assert step(4.99) == green
60+
assert step(5) == blue
61+
assert step(10) == blue
62+
assert step(20) == blue
63+
64+
65+
def test_linear_color_indexing():
66+
linear = cm.LinearColormap(
67+
colors=["black", "red", "lime", "blue"],
68+
index=[1, 2, 4, 5],
69+
)
70+
assert linear(1) == black
71+
assert linear(2) == red
72+
assert linear(4) == green
73+
assert linear(5) == blue
74+
assert linear(3) == "#7f7f00ff"
75+
76+
3177
def test_linear_to_step():
3278
some_list = [30.6, 50, 51, 52, 53, 54, 55, 60, 70, 100]
3379
lc = cm.linear.YlOrRd_06

0 commit comments

Comments
 (0)