From cc827876e957e9fd4c3392a453efabf55755433a Mon Sep 17 00:00:00 2001 From: Florence Haudin Date: Tue, 3 May 2022 18:00:36 +0200 Subject: [PATCH 1/3] Update the colormap HTML representation to match the way it renders on Folium maps. --- branca/colormap.py | 55 +++++++++++++++++++++++++--------- examples/Custom_colormap.ipynb | 45 ++++++++++++++++++++++++++++ 2 files changed, 86 insertions(+), 14 deletions(-) create mode 100644 examples/Custom_colormap.ipynb diff --git a/branca/colormap.py b/branca/colormap.py index 8db9583..b641aad 100644 --- a/branca/colormap.py +++ b/branca/colormap.py @@ -141,21 +141,48 @@ def __call__(self, x): return self.rgba_hex_str(x) def _repr_html_(self): + width = 500 + nb_ticks = 7 + delta_x = math.floor(width / (nb_ticks - 1)) + x_ticks = [(i) * delta_x for i in range(0, nb_ticks)] + delta_val = delta_x * (self.vmax - self.vmin) / width + val_ticks = [round(self.vmin + (i) * delta_val, 1) for i in range(0, nb_ticks)] + return ( - '' + - ''.join( - [('').format( - i=i*1, - color=self.rgba_hex_str( - self.vmin + - (self.vmax-self.vmin)*i/499.) - ) - for i in range(500)]) + - '{}'.format(self.vmin) + - '{}'.format( - self.vmax) + - '') + ''.format(width) + + "".join( + [ + ( + '' + ).format( + i=i * 1, + color=self.rgba_hex_str( + self.vmin + (self.vmax - self.vmin) * i / (width - 1) + ), + ) + for i in range(width) + ] + ) + + '{}'.format( + self.vmin + ) + + "".join( + [ + ( + '{}' + ).format(x_ticks[i], val_ticks[i]) + for i in range(1, nb_ticks - 1) + ] + ) + + '{}'.format( + width, self.vmax + ) + + '{}'.format( + self.caption + ) + + "" + ) class LinearColormap(ColorMap): diff --git a/examples/Custom_colormap.ipynb b/examples/Custom_colormap.ipynb new file mode 100644 index 0000000..ec7797e --- /dev/null +++ b/examples/Custom_colormap.ipynb @@ -0,0 +1,45 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "id": "1d544550", + "metadata": {}, + "outputs": [], + "source": [ + "from ipywidgets import Output\n", + "from branca.colormap import linear\n", + "\n", + "colormap_choice = linear.YlOrRd_04\n", + "widget = Output()\n", + "with widget:\n", + " vmin = 3.2\n", + " vmax = 10.3\n", + " colormap=colormap_choice.scale(vmin,vmax)\n", + " colormap.caption ='Unemployment rate'\n", + " display(colormap)" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.9.12" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} From 612051d059e513dd51de21d8494719e3ac6efd81 Mon Sep 17 00:00:00 2001 From: Frank <33519926+Conengmo@users.noreply.github.com> Date: Fri, 4 Nov 2022 15:07:14 +0100 Subject: [PATCH 2/3] add comment to _repr_html_ --- branca/colormap.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/branca/colormap.py b/branca/colormap.py index 02c571c..d3c7297 100644 --- a/branca/colormap.py +++ b/branca/colormap.py @@ -143,6 +143,11 @@ def __call__(self, x): return self.rgba_hex_str(x) def _repr_html_(self): + """Display the colormap in a Jupyter Notebook. + + Does not support all the class arguments. + + """ width = 500 nb_ticks = 7 delta_x = math.floor(width / (nb_ticks - 1)) From cbff98340488f69eb5abba3a040413b1f4e6fb4a Mon Sep 17 00:00:00 2001 From: Frank <33519926+Conengmo@users.noreply.github.com> Date: Fri, 4 Nov 2022 15:08:09 +0100 Subject: [PATCH 3/3] don't use ipyleaflet in notebook --- examples/Custom_colormap.ipynb | 33 ++++++++++++++++++++++----------- 1 file changed, 22 insertions(+), 11 deletions(-) diff --git a/examples/Custom_colormap.ipynb b/examples/Custom_colormap.ipynb index ec7797e..cc02b44 100644 --- a/examples/Custom_colormap.ipynb +++ b/examples/Custom_colormap.ipynb @@ -2,22 +2,33 @@ "cells": [ { "cell_type": "code", - "execution_count": null, + "execution_count": 1, "id": "1d544550", "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/html": [ + "3.24.45.66.77.99.110.3Unemployment rate" + ], + "text/plain": [ + "" + ] + }, + "execution_count": 1, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "from ipywidgets import Output\n", "from branca.colormap import linear\n", "\n", "colormap_choice = linear.YlOrRd_04\n", - "widget = Output()\n", - "with widget:\n", - " vmin = 3.2\n", - " vmax = 10.3\n", - " colormap=colormap_choice.scale(vmin,vmax)\n", - " colormap.caption ='Unemployment rate'\n", - " display(colormap)" + "vmin = 3.2\n", + "vmax = 10.3\n", + "colormap = colormap_choice.scale(vmin, vmax)\n", + "colormap.caption = 'Unemployment rate'\n", + "colormap" ] } ], @@ -37,7 +48,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.12" + "version": "3.9.13" } }, "nbformat": 4,