Skip to content

Commit 93fa3c4

Browse files
Simplify the logics of attributes of Heatmap class and compute vmin, vmax and colormap from gradient data (values and colors).
1 parent e8bbcc0 commit 93fa3c4

File tree

2 files changed

+103
-9
lines changed

2 files changed

+103
-9
lines changed

examples/Heatmap_with_colormap.ipynb

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": null,
6+
"id": "98958552-db61-4b15-9025-8483234118b3",
7+
"metadata": {},
8+
"outputs": [],
9+
"source": [
10+
"# Set up for JupyterLite\n",
11+
"try:\n",
12+
" import piplite\n",
13+
" await piplite.install('ipyleaflet')\n",
14+
"except ImportError:\n",
15+
" pass"
16+
]
17+
},
18+
{
19+
"cell_type": "code",
20+
"execution_count": null,
21+
"id": "974bc9b2-4a25-455b-8c4d-ca565e21f146",
22+
"metadata": {},
23+
"outputs": [],
24+
"source": [
25+
"import ipyleaflet\n",
26+
"from random import uniform\n",
27+
"import time\n",
28+
"from branca.colormap import linear, LinearColormap"
29+
]
30+
},
31+
{
32+
"cell_type": "code",
33+
"execution_count": null,
34+
"id": "fa27e0da-7047-4f06-93e8-3fb230e554d4",
35+
"metadata": {},
36+
"outputs": [],
37+
"source": [
38+
"def create_random_data(length):\n",
39+
" \"Return a list of some random lat/lon/value triples.\"\n",
40+
" return [\n",
41+
" [uniform(-80, 80), uniform(-180, 180), uniform(0, 1000)] for i in range(length)\n",
42+
" ]"
43+
]
44+
},
45+
{
46+
"cell_type": "code",
47+
"execution_count": null,
48+
"id": "cd1fc9dd-addf-4085-95ff-b8d4b983491f",
49+
"metadata": {},
50+
"outputs": [],
51+
"source": [
52+
"m = ipyleaflet.Map(center=[0, 0], zoom=2)\n",
53+
"heat = ipyleaflet.Heatmap(locations=create_random_data(1000), radius=20, blur=10, gradient={0.25 : 'orange', 0.85 : 'red'})\n",
54+
"colormap_control = ipyleaflet.ColormapControl(\n",
55+
" caption='Intensity',\n",
56+
" colormap=heat.colormap,\n",
57+
" value_min=heat.vmin,\n",
58+
" value_max=heat.vmax,\n",
59+
" position='topright',\n",
60+
" transparent_bg=True,\n",
61+
" )\n",
62+
"m.add(heat)\n",
63+
"m.add(colormap_control)"
64+
]
65+
}
66+
],
67+
"metadata": {
68+
"kernelspec": {
69+
"display_name": "Python 3 (ipykernel)",
70+
"language": "python",
71+
"name": "python3"
72+
},
73+
"language_info": {
74+
"codemirror_mode": {
75+
"name": "ipython",
76+
"version": 3
77+
},
78+
"file_extension": ".py",
79+
"mimetype": "text/x-python",
80+
"name": "python",
81+
"nbconvert_exporter": "python",
82+
"pygments_lexer": "ipython3",
83+
"version": "3.9.13"
84+
}
85+
},
86+
"nbformat": 4,
87+
"nbformat_minor": 5
88+
}

ipyleaflet/leaflet.py

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -823,15 +823,21 @@ class Heatmap(RasterLayer):
823823
radius = Float(25.0).tag(sync=True, o=True)
824824
blur = Float(15.0).tag(sync=True, o=True)
825825
gradient = Dict({0.4: 'blue', 0.6: 'cyan', 0.7: 'lime', 0.8: 'yellow', 1.0: 'red'}).tag(sync=True, o=True)
826-
colors = ['blue', 'cyan', 'lime', 'yellow', 'red']
827-
values = [0.4, 0.6, 0.7, 0.8, 1.0]
828-
dict = {}
829-
for i in range(len(values)):
830-
dict[values[i]] = colors[i]
831-
vmin = values[0]
832-
vmax = values[len(values) - 1]
833-
gradient = Dict(dict).tag(sync=True, o=True)
834-
colormap = LinearColormap(colors, vmin=vmin, vmax=vmax)
826+
827+
def __init__(self, **kwargs):
828+
super(Heatmap, self).__init__(**kwargs)
829+
self.data = self._get_data()
830+
831+
@observe('color_mapping')
832+
def _updata_data(self, change):
833+
self.data = self._get_data()
834+
835+
def _get_data(self):
836+
self.values = list(self.gradient.keys())
837+
self.colors = list(self.gradient.values())
838+
self.vmin = self.values[0]
839+
self.vmax = self.values[len(self.values) - 1]
840+
self.colormap = LinearColormap(self.colors, vmin=self.vmin, vmax=self.vmax)
835841

836842

837843
class VectorTileLayer(Layer):

0 commit comments

Comments
 (0)