Skip to content

Commit 8ed9839

Browse files
ThomasBurThomasBurpre-commit-ci[bot]Conengmo
authored
ColorMap text color (#160)
* Add ability to specify text_color for ColorMap. * Modify colormap example to include text color. * Make text_color comment clearer. * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Return executionCount to 1 * Use implied line contrinuation to shorten lines * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Apply black autoformatting. * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * ENH: add ability to change color of legend's text * Minor fixes for new text_color argument * add default value to docstring --------- Co-authored-by: ThomasBur <thomasbur@outlook> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Frank <[email protected]>
1 parent 4fba6e8 commit 8ed9839

File tree

3 files changed

+50
-9
lines changed

3 files changed

+50
-9
lines changed

branca/colormap.py

Lines changed: 44 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -72,19 +72,29 @@ class ColorMap(MacroElement):
7272
The right bound of the color scale.
7373
caption: str
7474
A caption to draw with the colormap.
75+
text_color: str, default "black"
76+
The color for the text.
7577
max_labels : int, default 10
7678
Maximum number of legend tick labels
7779
"""
7880

7981
_template = ENV.get_template("color_scale.js")
8082

81-
def __init__(self, vmin=0.0, vmax=1.0, caption="", max_labels=10):
83+
def __init__(
84+
self,
85+
vmin=0.0,
86+
vmax=1.0,
87+
caption="",
88+
text_color="black",
89+
max_labels=10,
90+
):
8291
super().__init__()
8392
self._name = "ColorMap"
8493

8594
self.vmin = vmin
8695
self.vmax = vmax
8796
self.caption = caption
97+
self.text_color = text_color
8898
self.index = [vmin, vmax]
8999
self.max_labels = max_labels
90100
self.tick_labels = None
@@ -185,22 +195,32 @@ def _repr_html_(self):
185195
for i in range(self.width)
186196
],
187197
)
188-
+ '<text x="0" y="38" style="text-anchor:start; font-size:11px; font:Arial">{}</text>'.format( # noqa
198+
+ (
199+
'<text x="0" y="38" style="text-anchor:start; font-size:11px;'
200+
' font:Arial; fill:{}">{}</text>'
201+
).format(
202+
self.text_color,
189203
self.vmin,
190204
)
191205
+ "".join(
192206
[
193207
(
194-
'<text x="{}" y="38"; style="text-anchor:middle; font-size:11px; font:Arial">{}</text>' # noqa
195-
).format(x_ticks[i], val_ticks[i])
208+
'<text x="{}" y="38"; style="text-anchor:middle; font-size:11px;'
209+
' font:Arial; fill:{}">{}</text>'
210+
).format(x_ticks[i], self.text_color, val_ticks[i])
196211
for i in range(1, nb_ticks - 1)
197212
],
198213
)
199-
+ '<text x="{}" y="38" style="text-anchor:end; font-size:11px; font:Arial">{}</text>'.format(
214+
+ (
215+
'<text x="{}" y="38" style="text-anchor:end; font-size:11px;'
216+
' font:Arial; fill:{}">{}</text>'
217+
).format(
200218
self.width,
219+
self.text_color,
201220
self.vmax,
202221
)
203-
+ '<text x="0" y="12" style="font-size:11px; font:Arial">{}</text>'.format(
222+
+ '<text x="0" y="12" style="font-size:11px; font:Arial; fill:{}">{}</text>'.format(
223+
self.text_color,
204224
self.caption,
205225
)
206226
+ "</svg>"
@@ -233,6 +253,10 @@ class LinearColormap(ColorMap):
233253
vmax : float, default 1.
234254
The maximal value for the colormap.
235255
Values higher than `vmax` will be bound directly to `colors[-1]`.
256+
caption: str
257+
A caption to draw with the colormap.
258+
text_color: str, default "black"
259+
The color for the text.
236260
max_labels : int, default 10
237261
Maximum number of legend tick labels
238262
tick_labels: list of floats, default None
@@ -245,13 +269,15 @@ def __init__(
245269
vmin=0.0,
246270
vmax=1.0,
247271
caption="",
272+
text_color="black",
248273
max_labels=10,
249274
tick_labels=None,
250275
):
251276
super().__init__(
252277
vmin=vmin,
253278
vmax=vmax,
254279
caption=caption,
280+
text_color=text_color,
255281
max_labels=max_labels,
256282
)
257283
self.tick_labels = tick_labels
@@ -415,13 +441,15 @@ def to_step(
415441
]
416442

417443
caption = self.caption
444+
text_color = self.text_color
418445

419446
return StepColormap(
420447
colors,
421448
index=index,
422449
vmin=index[0],
423450
vmax=index[-1],
424451
caption=caption,
452+
text_color=text_color,
425453
max_labels=max_labels,
426454
tick_labels=self.tick_labels,
427455
)
@@ -439,6 +467,7 @@ def scale(self, vmin=0.0, vmax=1.0, max_labels=10):
439467
vmin=vmin,
440468
vmax=vmax,
441469
caption=self.caption,
470+
text_color=self.text_color,
442471
max_labels=max_labels,
443472
)
444473

@@ -469,6 +498,10 @@ class StepColormap(ColorMap):
469498
vmax : float, default 1.
470499
The maximal value for the colormap.
471500
Values higher than `vmax` will be bound directly to `colors[-1]`.
501+
caption: str
502+
A caption to draw with the colormap.
503+
text_color: str, default "black"
504+
The color for the text.
472505
max_labels : int, default 10
473506
Maximum number of legend tick labels
474507
tick_labels: list of floats, default None
@@ -482,13 +515,15 @@ def __init__(
482515
vmin=0.0,
483516
vmax=1.0,
484517
caption="",
518+
text_color="black",
485519
max_labels=10,
486520
tick_labels=None,
487521
):
488522
super().__init__(
489523
vmin=vmin,
490524
vmax=vmax,
491525
caption=caption,
526+
text_color=text_color,
492527
max_labels=max_labels,
493528
)
494529
self.tick_labels = tick_labels
@@ -544,6 +579,8 @@ def to_linear(self, index=None, max_labels=10):
544579
index=index,
545580
vmin=self.vmin,
546581
vmax=self.vmax,
582+
caption=self.caption,
583+
text_color=self.text_color,
547584
max_labels=max_labels,
548585
)
549586

@@ -560,6 +597,7 @@ def scale(self, vmin=0.0, vmax=1.0, max_labels=10):
560597
vmin=vmin,
561598
vmax=vmax,
562599
caption=self.caption,
600+
text_color=self.text_color,
563601
max_labels=max_labels,
564602
)
565603

branca/templates/color_scale.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232

3333
{{this.get_name()}}.g = {{this.get_name()}}.svg.append("g")
3434
.attr("class", "key")
35+
.attr("fill", {{ this.text_color | tojson }})
3536
.attr("transform", "translate(25,16)");
3637

3738
{{this.get_name()}}.g.selectAll("rect")
@@ -51,5 +52,6 @@
5152
{{this.get_name()}}.g.call({{this.get_name()}}.xAxis).append("text")
5253
.attr("class", "caption")
5354
.attr("y", 21)
55+
.attr("fill", {{ this.text_color | tojson }})
5456
.text({{ this.caption|tojson }});
5557
{% endmacro %}

examples/Custom_colormap.ipynb

Lines changed: 4 additions & 3 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)