From 4f5e5e66ff9f76f2fed26e93dbd9a391b3167aaf Mon Sep 17 00:00:00 2001 From: ThomasBur Date: Fri, 29 Mar 2024 17:05:03 +1100 Subject: [PATCH 01/12] Add ability to specify text_color for ColorMap. --- branca/colormap.py | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/branca/colormap.py b/branca/colormap.py index b622b3a..33bdf73 100644 --- a/branca/colormap.py +++ b/branca/colormap.py @@ -72,19 +72,22 @@ class ColorMap(MacroElement): The right bound of the color scale. caption: str A caption to draw with the colormap. + text_color: str + A 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 @@ -181,22 +184,25 @@ def _repr_html_(self): for i in range(self.width) ], ) - + '{}'.format( # noqa + + '{}'.format( # noqa + self.text_color, self.vmin, ) + "".join( [ ( - '{}' # noqa - ).format(x_ticks[i], val_ticks[i]) + '{}' # noqa + ).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, ) + "" @@ -241,6 +247,7 @@ def __init__( vmin=0.0, vmax=1.0, caption="", + text_color="", max_labels=10, tick_labels=None, ): @@ -248,6 +255,7 @@ def __init__( vmin=vmin, vmax=vmax, caption=caption, + text_color=text_color, max_labels=max_labels, ) self.tick_labels = tick_labels @@ -411,6 +419,7 @@ def to_step( ] caption = self.caption + text_color = self.text_color return StepColormap( colors, @@ -418,6 +427,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, ) @@ -435,6 +445,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, ) @@ -478,6 +489,7 @@ def __init__( vmin=0.0, vmax=1.0, caption="", + text_color="", max_labels=10, tick_labels=None, ): @@ -485,6 +497,7 @@ def __init__( vmin=vmin, vmax=vmax, caption=caption, + text_color=text_color, max_labels=max_labels, ) self.tick_labels = tick_labels @@ -556,6 +569,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, ) From a8803f4688d6acdfe16615ccf809f55b0921867f Mon Sep 17 00:00:00 2001 From: ThomasBur Date: Fri, 29 Mar 2024 17:17:25 +1100 Subject: [PATCH 02/12] Modify colormap example to include text color. --- examples/Custom_colormap.ipynb | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/examples/Custom_colormap.ipynb b/examples/Custom_colormap.ipynb index cc02b44..5249732 100644 --- a/examples/Custom_colormap.ipynb +++ b/examples/Custom_colormap.ipynb @@ -2,20 +2,20 @@ "cells": [ { "cell_type": "code", - "execution_count": 1, + "execution_count": 8, "id": "1d544550", "metadata": {}, "outputs": [ { "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, + "execution_count": 8, "metadata": {}, "output_type": "execute_result" } @@ -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, From 4c635f790e018c086d5f625c4ffbce917a6a7c2d Mon Sep 17 00:00:00 2001 From: ThomasBur Date: Fri, 29 Mar 2024 17:24:32 +1100 Subject: [PATCH 03/12] Make text_color comment clearer. --- branca/colormap.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/branca/colormap.py b/branca/colormap.py index 33bdf73..70c7187 100644 --- a/branca/colormap.py +++ b/branca/colormap.py @@ -73,7 +73,7 @@ class ColorMap(MacroElement): caption: str A caption to draw with the colormap. text_color: str - A color for the text. + The color for the text. max_labels : int, default 10 Maximum number of legend tick labels """ From b31032c294a2d4a7faf0479768efb48929a6ec0a Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 29 Mar 2024 06:39:15 +0000 Subject: [PATCH 04/12] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- branca/colormap.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/branca/colormap.py b/branca/colormap.py index 70c7187..639dfeb 100644 --- a/branca/colormap.py +++ b/branca/colormap.py @@ -80,7 +80,9 @@ class ColorMap(MacroElement): _template = ENV.get_template("color_scale.js") - def __init__(self, vmin=0.0, vmax=1.0, caption="", text_color="black", max_labels=10): + def __init__( + self, vmin=0.0, vmax=1.0, caption="", text_color="black", max_labels=10, + ): super().__init__() self._name = "ColorMap" From 3b2953a3fcf6bca943c658e18d4f96ad820cf6c6 Mon Sep 17 00:00:00 2001 From: ThomasBur Date: Fri, 29 Mar 2024 18:38:36 +1100 Subject: [PATCH 05/12] Return executionCount to 1 --- examples/Custom_colormap.ipynb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/examples/Custom_colormap.ipynb b/examples/Custom_colormap.ipynb index 5249732..da770c3 100644 --- a/examples/Custom_colormap.ipynb +++ b/examples/Custom_colormap.ipynb @@ -2,7 +2,7 @@ "cells": [ { "cell_type": "code", - "execution_count": 8, + "execution_count": 1, "id": "1d544550", "metadata": {}, "outputs": [ @@ -12,10 +12,10 @@ "3.24.45.66.87.99.110.3Unemployment rate" ], "text/plain": [ - "" + "" ] }, - "execution_count": 8, + "execution_count": 1, "metadata": {}, "output_type": "execute_result" } From 5320fa71f7c31079bb640bfc3cd646cf8b488289 Mon Sep 17 00:00:00 2001 From: ThomasBur Date: Fri, 29 Mar 2024 18:39:30 +1100 Subject: [PATCH 06/12] Use implied line contrinuation to shorten lines --- branca/colormap.py | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/branca/colormap.py b/branca/colormap.py index 639dfeb..1b4afb3 100644 --- a/branca/colormap.py +++ b/branca/colormap.py @@ -80,9 +80,7 @@ class ColorMap(MacroElement): _template = ENV.get_template("color_scale.js") - def __init__( - self, vmin=0.0, vmax=1.0, caption="", text_color="black", max_labels=10, - ): + def __init__(self, vmin=0.0, vmax=1.0, caption="", text_color="black", max_labels=10): super().__init__() self._name = "ColorMap" @@ -186,19 +184,22 @@ def _repr_html_(self): for i in range(self.width) ], ) - + '{}'.format( # noqa + + ('{}').format( self.text_color, self.vmin, ) + "".join( [ ( - '{}' # noqa + '{}' ).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, From 526607d77d6e0f0f6fa4b866c6306403fb753d77 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 29 Mar 2024 07:40:17 +0000 Subject: [PATCH 07/12] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- branca/colormap.py | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/branca/colormap.py b/branca/colormap.py index 1b4afb3..e11b601 100644 --- a/branca/colormap.py +++ b/branca/colormap.py @@ -80,7 +80,9 @@ class ColorMap(MacroElement): _template = ENV.get_template("color_scale.js") - def __init__(self, vmin=0.0, vmax=1.0, caption="", text_color="black", max_labels=10): + def __init__( + self, vmin=0.0, vmax=1.0, caption="", text_color="black", max_labels=10, + ): super().__init__() self._name = "ColorMap" @@ -184,8 +186,10 @@ def _repr_html_(self): for i in range(self.width) ], ) - + ('{}').format( + + ( + '{}' + ).format( self.text_color, self.vmin, ) @@ -198,8 +202,10 @@ def _repr_html_(self): for i in range(1, nb_ticks - 1) ], ) - + ('{}').format( + + ( + '{}' + ).format( self.width, self.text_color, self.vmax, From 20b9c683fdfa80bf4cfc677a8247088653c5be1d Mon Sep 17 00:00:00 2001 From: ThomasBur Date: Fri, 29 Mar 2024 19:15:45 +1100 Subject: [PATCH 08/12] Apply black autoformatting. --- branca/colormap.py | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/branca/colormap.py b/branca/colormap.py index e11b601..d354f83 100644 --- a/branca/colormap.py +++ b/branca/colormap.py @@ -81,7 +81,12 @@ class ColorMap(MacroElement): _template = ENV.get_template("color_scale.js") def __init__( - self, vmin=0.0, vmax=1.0, caption="", text_color="black", max_labels=10, + self, + vmin=0.0, + vmax=1.0, + caption="", + text_color="black", + max_labels=10, ): super().__init__() self._name = "ColorMap" @@ -372,7 +377,9 @@ def to_step( method = ( "quantiles" if quantiles is not None - else method if method is not None else "linear" + else method + if method is not None + else "linear" ) if method.lower().startswith("lin"): if n is None: From 37ff87abe325d769a22bb898bacd44652ad67a24 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 29 Mar 2024 08:16:00 +0000 Subject: [PATCH 09/12] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- branca/colormap.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/branca/colormap.py b/branca/colormap.py index d354f83..58bc0b2 100644 --- a/branca/colormap.py +++ b/branca/colormap.py @@ -377,9 +377,7 @@ def to_step( method = ( "quantiles" if quantiles is not None - else method - if method is not None - else "linear" + else method if method is not None else "linear" ) if method.lower().startswith("lin"): if n is None: From 117c3ffde941ee1db9e2697dd8b8f9fecd86c607 Mon Sep 17 00:00:00 2001 From: ThomasBur Date: Thu, 4 Apr 2024 19:44:58 +1100 Subject: [PATCH 10/12] ENH: add ability to change color of legend's text --- branca/templates/color_scale.js | 2 ++ 1 file changed, 2 insertions(+) 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 %} From 597488ae4234f14558524e698a7a34701a77a002 Mon Sep 17 00:00:00 2001 From: ThomasBur Date: Thu, 4 Apr 2024 19:48:51 +1100 Subject: [PATCH 11/12] Minor fixes for new text_color argument --- branca/colormap.py | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/branca/colormap.py b/branca/colormap.py index 58bc0b2..0e885a7 100644 --- a/branca/colormap.py +++ b/branca/colormap.py @@ -249,6 +249,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 + The color for the text. max_labels : int, default 10 Maximum number of legend tick labels tick_labels: list of floats, default None @@ -261,7 +265,7 @@ def __init__( vmin=0.0, vmax=1.0, caption="", - text_color="", + text_color="black", max_labels=10, tick_labels=None, ): @@ -490,6 +494,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 + The color for the text. max_labels : int, default 10 Maximum number of legend tick labels tick_labels: list of floats, default None @@ -503,7 +511,7 @@ def __init__( vmin=0.0, vmax=1.0, caption="", - text_color="", + text_color="black", max_labels=10, tick_labels=None, ): @@ -567,6 +575,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, ) From b311fd261729f9c9d4bd269745f48ee3af3cbc9b Mon Sep 17 00:00:00 2001 From: Frank <33519926+Conengmo@users.noreply.github.com> Date: Mon, 6 May 2024 19:13:12 +0200 Subject: [PATCH 12/12] add default value to docstring --- branca/colormap.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/branca/colormap.py b/branca/colormap.py index e83e88f..e5dd839 100644 --- a/branca/colormap.py +++ b/branca/colormap.py @@ -72,7 +72,7 @@ class ColorMap(MacroElement): The right bound of the color scale. caption: str A caption to draw with the colormap. - text_color: str + text_color: str, default "black" The color for the text. max_labels : int, default 10 Maximum number of legend tick labels @@ -255,7 +255,7 @@ class LinearColormap(ColorMap): Values higher than `vmax` will be bound directly to `colors[-1]`. caption: str A caption to draw with the colormap. - text_color: str + text_color: str, default "black" The color for the text. max_labels : int, default 10 Maximum number of legend tick labels @@ -500,7 +500,7 @@ class StepColormap(ColorMap): Values higher than `vmax` will be bound directly to `colors[-1]`. caption: str A caption to draw with the colormap. - text_color: str + text_color: str, default "black" The color for the text. max_labels : int, default 10 Maximum number of legend tick labels