diff --git a/branca/colormap.py b/branca/colormap.py index bf3cc7a..e5dd839 100644 --- a/branca/colormap.py +++ b/branca/colormap.py @@ -72,19 +72,29 @@ class ColorMap(MacroElement): The right bound of the color scale. caption: str A caption to draw with the colormap. + text_color: str, default "black" + The color for the text. max_labels : int, default 10 Maximum number of legend tick labels """ _template = ENV.get_template("color_scale.js") - def __init__(self, vmin=0.0, vmax=1.0, caption="", max_labels=10): + def __init__( + self, + vmin=0.0, + vmax=1.0, + caption="", + text_color="black", + max_labels=10, + ): super().__init__() self._name = "ColorMap" self.vmin = vmin self.vmax = vmax self.caption = caption + self.text_color = text_color self.index = [vmin, vmax] self.max_labels = max_labels self.tick_labels = None @@ -185,22 +195,32 @@ def _repr_html_(self): for i in range(self.width) ], ) - + '{}'.format( # noqa + + ( + '{}' + ).format( + self.text_color, self.vmin, ) + "".join( [ ( - '{}' # noqa - ).format(x_ticks[i], val_ticks[i]) + '{}' + ).format(x_ticks[i], self.text_color, val_ticks[i]) for i in range(1, nb_ticks - 1) ], ) - + '{}'.format( + + ( + '{}' + ).format( self.width, + self.text_color, self.vmax, ) - + '{}'.format( + + '{}'.format( + self.text_color, self.caption, ) + "" @@ -233,6 +253,10 @@ class LinearColormap(ColorMap): vmax : float, default 1. The maximal value for the colormap. Values higher than `vmax` will be bound directly to `colors[-1]`. + caption: str + A caption to draw with the colormap. + text_color: str, default "black" + The color for the text. max_labels : int, default 10 Maximum number of legend tick labels tick_labels: list of floats, default None @@ -245,6 +269,7 @@ def __init__( vmin=0.0, vmax=1.0, caption="", + text_color="black", max_labels=10, tick_labels=None, ): @@ -252,6 +277,7 @@ def __init__( vmin=vmin, vmax=vmax, caption=caption, + text_color=text_color, max_labels=max_labels, ) self.tick_labels = tick_labels @@ -415,6 +441,7 @@ def to_step( ] caption = self.caption + text_color = self.text_color return StepColormap( colors, @@ -422,6 +449,7 @@ def to_step( vmin=index[0], vmax=index[-1], caption=caption, + text_color=text_color, max_labels=max_labels, tick_labels=self.tick_labels, ) @@ -439,6 +467,7 @@ def scale(self, vmin=0.0, vmax=1.0, max_labels=10): vmin=vmin, vmax=vmax, caption=self.caption, + text_color=self.text_color, max_labels=max_labels, ) @@ -469,6 +498,10 @@ class StepColormap(ColorMap): vmax : float, default 1. The maximal value for the colormap. Values higher than `vmax` will be bound directly to `colors[-1]`. + caption: str + A caption to draw with the colormap. + text_color: str, default "black" + The color for the text. max_labels : int, default 10 Maximum number of legend tick labels tick_labels: list of floats, default None @@ -482,6 +515,7 @@ def __init__( vmin=0.0, vmax=1.0, caption="", + text_color="black", max_labels=10, tick_labels=None, ): @@ -489,6 +523,7 @@ def __init__( vmin=vmin, vmax=vmax, caption=caption, + text_color=text_color, max_labels=max_labels, ) self.tick_labels = tick_labels @@ -544,6 +579,8 @@ def to_linear(self, index=None, max_labels=10): index=index, vmin=self.vmin, vmax=self.vmax, + caption=self.caption, + text_color=self.text_color, max_labels=max_labels, ) @@ -560,6 +597,7 @@ def scale(self, vmin=0.0, vmax=1.0, max_labels=10): vmin=vmin, vmax=vmax, caption=self.caption, + text_color=self.text_color, max_labels=max_labels, ) diff --git a/branca/templates/color_scale.js b/branca/templates/color_scale.js index 18cd01a..c49fd24 100644 --- a/branca/templates/color_scale.js +++ b/branca/templates/color_scale.js @@ -32,6 +32,7 @@ {{this.get_name()}}.g = {{this.get_name()}}.svg.append("g") .attr("class", "key") + .attr("fill", {{ this.text_color | tojson }}) .attr("transform", "translate(25,16)"); {{this.get_name()}}.g.selectAll("rect") @@ -51,5 +52,6 @@ {{this.get_name()}}.g.call({{this.get_name()}}.xAxis).append("text") .attr("class", "caption") .attr("y", 21) + .attr("fill", {{ this.text_color | tojson }}) .text({{ this.caption|tojson }}); {% endmacro %} diff --git a/examples/Custom_colormap.ipynb b/examples/Custom_colormap.ipynb index cc02b44..da770c3 100644 --- a/examples/Custom_colormap.ipynb +++ b/examples/Custom_colormap.ipynb @@ -9,10 +9,10 @@ { "data": { "text/html": [ - "3.24.45.66.77.99.110.3Unemployment rate" + "3.24.45.66.87.99.110.3Unemployment rate" ], "text/plain": [ - "" + "" ] }, "execution_count": 1, @@ -28,6 +28,7 @@ "vmax = 10.3\n", "colormap = colormap_choice.scale(vmin, vmax)\n", "colormap.caption = 'Unemployment rate'\n", + "colormap.text_color = 'cyan'\n", "colormap" ] } @@ -48,7 +49,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.10.12" } }, "nbformat": 4,