Skip to content

Commit 1a4b712

Browse files
authored
ENH: expose max_labels in ColorMap (#90)
* ENH: expose max_lables * lint * docstring * revert addition of the last value * tests * better error handling
1 parent a872bef commit 1a4b712

File tree

2 files changed

+55
-13
lines changed

2 files changed

+55
-13
lines changed

branca/colormap.py

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -70,24 +70,27 @@ class ColorMap(MacroElement):
7070
The right bound of the color scale.
7171
caption: str
7272
A caption to draw with the colormap.
73+
max_labels : int, default 10
74+
Maximum number of legend tick labels
7375
"""
7476
_template = ENV.get_template('color_scale.js')
7577

76-
def __init__(self, vmin=0., vmax=1., caption=''):
78+
def __init__(self, vmin=0., vmax=1., caption='', max_labels=10):
7779
super(ColorMap, self).__init__()
7880
self._name = 'ColorMap'
7981

8082
self.vmin = vmin
8183
self.vmax = vmax
8284
self.caption = caption
8385
self.index = [vmin, vmax]
86+
self.max_labels = max_labels
8487

8588
def render(self, **kwargs):
8689
"""Renders the HTML representation of the element."""
8790
self.color_domain = [self.vmin + (self.vmax-self.vmin) * k/499. for
8891
k in range(500)]
8992
self.color_range = [self.__call__(x) for x in self.color_domain]
90-
self.tick_labels = legend_scaler(self.index)
93+
self.tick_labels = legend_scaler(self.index, self.max_labels)
9194

9295
super(ColorMap, self).render(**kwargs)
9396

@@ -180,11 +183,13 @@ class LinearColormap(ColorMap):
180183
Values lower than `vmin` will be bound directly to `colors[0]`.
181184
vmax : float, default 1.
182185
The maximal value for the colormap.
183-
Values higher than `vmax` will be bound directly to `colors[-1]`."""
186+
Values higher than `vmax` will be bound directly to `colors[-1]`.
187+
max_labels : int, default 10
188+
Maximum number of legend tick labels"""
184189

185-
def __init__(self, colors, index=None, vmin=0., vmax=1., caption=''):
190+
def __init__(self, colors, index=None, vmin=0., vmax=1., caption='', max_labels=10):
186191
super(LinearColormap, self).__init__(vmin=vmin, vmax=vmax,
187-
caption=caption)
192+
caption=caption, max_labels=max_labels)
188193

189194
n = len(colors)
190195
if n < 2:
@@ -216,7 +221,7 @@ def rgba_floats_tuple(self, x):
216221
in range(4))
217222

218223
def to_step(self, n=None, index=None, data=None, method=None,
219-
quantiles=None, round_method=None):
224+
quantiles=None, round_method=None, max_labels=10):
220225
"""Splits the LinearColormap into a StepColormap.
221226
222227
Parameters
@@ -243,6 +248,8 @@ def to_step(self, n=None, index=None, data=None, method=None,
243248
* If 'log10', all values will be rounded to the nearest
244249
order-of-magnitude integer. For example, 2100 is rounded to
245250
2000, 2790 to 3000.
251+
max_labels : int, default 10
252+
Maximum number of legend tick labels
246253
247254
Returns
248255
-------
@@ -324,9 +331,10 @@ def to_step(self, n=None, index=None, data=None, method=None,
324331

325332
caption = self.caption
326333

327-
return StepColormap(colors, index=index, vmin=index[0], vmax=index[-1], caption=caption)
334+
return StepColormap(colors, index=index, vmin=index[0], vmax=index[-1], caption=caption,
335+
max_labels=max_labels)
328336

329-
def scale(self, vmin=0., vmax=1.):
337+
def scale(self, vmin=0., vmax=1., max_labels=10):
330338
"""Transforms the colorscale so that the minimal and maximal values
331339
fit the given parameters.
332340
"""
@@ -336,6 +344,7 @@ def scale(self, vmin=0., vmax=1.):
336344
vmin=vmin,
337345
vmax=vmax,
338346
caption=self.caption,
347+
max_labels=max_labels
339348
)
340349

341350

@@ -364,11 +373,13 @@ class StepColormap(ColorMap):
364373
vmax : float, default 1.
365374
The maximal value for the colormap.
366375
Values higher than `vmax` will be bound directly to `colors[-1]`.
376+
max_labels : int, default 10
377+
Maximum number of legend tick labels
367378
368379
"""
369-
def __init__(self, colors, index=None, vmin=0., vmax=1., caption=''):
380+
def __init__(self, colors, index=None, vmin=0., vmax=1., caption='', max_labels=10):
370381
super(StepColormap, self).__init__(vmin=vmin, vmax=vmax,
371-
caption=caption)
382+
caption=caption, max_labels=max_labels)
372383

373384
n = len(colors)
374385
if n < 1:
@@ -393,7 +404,7 @@ def rgba_floats_tuple(self, x):
393404
i = len([u for u in self.index if u < x]) # 0 < i < n.
394405
return tuple(self.colors[i-1])
395406

396-
def to_linear(self, index=None):
407+
def to_linear(self, index=None, max_labels=10):
397408
"""
398409
Transforms the StepColormap into a LinearColormap.
399410
@@ -403,6 +414,8 @@ def to_linear(self, index=None):
403414
The values corresponding to each color in the output colormap.
404415
It has to be sorted.
405416
If None, a regular grid between `vmin` and `vmax` is created.
417+
max_labels : int, default 10
418+
Maximum number of legend tick labels
406419
407420
"""
408421
if index is None:
@@ -412,9 +425,9 @@ def to_linear(self, index=None):
412425

413426
colors = [self.rgba_floats_tuple(x) for x in index]
414427
return LinearColormap(colors, index=index,
415-
vmin=self.vmin, vmax=self.vmax)
428+
vmin=self.vmin, vmax=self.vmax, max_labels=max_labels)
416429

417-
def scale(self, vmin=0., vmax=1.):
430+
def scale(self, vmin=0., vmax=1., max_labels=10):
418431
"""Transforms the colorscale so that the minimal and maximal values
419432
fit the given parameters.
420433
"""
@@ -424,6 +437,7 @@ def scale(self, vmin=0., vmax=1.):
424437
vmin=vmin,
425438
vmax=vmax,
426439
caption=self.caption,
440+
max_labels=max_labels
427441
)
428442

429443

tests/test_colormap.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
----------------------
55
"""
66
import branca.colormap as cm
7+
import pytest
78

89

910
def test_simple_step():
@@ -55,3 +56,30 @@ def test_step_object():
5556
cm.step.PuBu_06.to_linear()
5657
cm.step.YlGn_06.scale(3, 12)
5758
cm.step._repr_html_()
59+
60+
@pytest.mark.parametrize("max_labels,expected", [
61+
(10, [0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0]),
62+
(5, [0.0, '', 2.0, '', 4.0, '', 6.0, '', 8.0, '']),
63+
(3, [0.0, '', '', '', 4.0, '', '', '', 8.0, '', '', '']),
64+
])
65+
def test_max_labels_linear(max_labels, expected):
66+
colorbar = cm.LinearColormap(['red'] * 10, vmin=0, vmax=9, max_labels=max_labels)
67+
try:
68+
colorbar.render()
69+
except AssertionError: # rendering outside parent Figure raises error
70+
pass
71+
assert colorbar.tick_labels == expected
72+
73+
74+
@pytest.mark.parametrize("max_labels,expected", [
75+
(10, [0.0, '', 2.0, '', 4.0, '', 6.0, '', 8.0, '', 10.0, '']),
76+
(5, [0.0, '', '', 3.0, '', '', 6.0, '', '', 9.0, '', '']),
77+
(3, [0.0, '', '', '', 4.0, '', '', '', 8.0, '', '', '']),
78+
])
79+
def test_max_labels_step(max_labels, expected):
80+
colorbar = cm.StepColormap(['red', 'blue'] * 5, vmin=0, vmax=10, max_labels=max_labels)
81+
try:
82+
colorbar.render()
83+
except AssertionError: # rendering outside parent Figure raises error
84+
pass
85+
assert colorbar.tick_labels == expected

0 commit comments

Comments
 (0)