Skip to content

Commit bbf57a2

Browse files
jtbakerConengmo
authored andcommitted
Added gradient to HeatMapWithTime. (#925)
Add `gradient` argument to the `HeatMapWithTime` class. With it users can provide a custom color scheme.
1 parent 06b73f5 commit bbf57a2

File tree

3 files changed

+57
-45
lines changed

3 files changed

+57
-45
lines changed

CHANGES.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
- Added `duration` option to `TimestampedGeoJson` (andy23512 #894)
2121
- Added `zoom_control` to `Map` to toggle zoom controls as per enhancement (#795) (okomarov #899)
2222
- Change default `date_options` in TimestampedGeoJson (andy23512 #914)
23+
- Added gradient argument to HeatMapWithTime (jtbaker #925)
2324

2425
API changes
2526

folium/plugins/heat_map_withtime.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ class HeatMapWithTime(Layer):
2929
The maximum opacity for the heatmap.
3030
scale_radius: default False
3131
Scale the radius of the points based on the zoom level.
32+
gradient: dict, default None
33+
Match point density values to colors. Color can be a name ('red'),
34+
RGB values ('rgb(255,0,0)') or a hex number ('#FF0000').
3235
use_local_extrema: default False
3336
Defines whether the heatmap uses a global extrema set found from the input data
3437
OR a local extrema (the maximum and minimum of the currently displayed view).
@@ -92,7 +95,8 @@ class HeatMapWithTime(Layer):
9295
maxOpacity: {{this.max_opacity}},
9396
scaleRadius: {{this.scale_radius}},
9497
useLocalExtrema: {{this.use_local_extrema}},
95-
defaultWeight: 1 ,
98+
defaultWeight: 1,
99+
{% if this.gradient %}gradient: {{ this.gradient }}{% endif %}
96100
}
97101
})
98102
.addTo({{this._parent.get_name()}});
@@ -101,10 +105,11 @@ class HeatMapWithTime(Layer):
101105
""")
102106

103107
def __init__(self, data, index=None, name=None, radius=15, min_opacity=0,
104-
max_opacity=0.6, scale_radius=False, use_local_extrema=False,
105-
auto_play=False, display_index=True, index_steps=1,
106-
min_speed=0.1, max_speed=10, speed_step=0.1,
107-
position='bottomleft', overlay=True, control=True, show=True):
108+
max_opacity=0.6, scale_radius=False, gradient=None,
109+
use_local_extrema=False, auto_play=False,
110+
display_index=True, index_steps=1, min_speed=0.1,
111+
max_speed=10, speed_step=0.1, position='bottomleft',
112+
overlay=True, control=True, show=True):
108113
super(HeatMapWithTime, self).__init__(name=name, overlay=overlay,
109114
control=control, show=show)
110115
self._name = 'HeatMap'
@@ -124,6 +129,7 @@ def __init__(self, data, index=None, name=None, radius=15, min_opacity=0,
124129
self.max_opacity = max_opacity
125130
self.scale_radius = 'true' if scale_radius else 'false'
126131
self.use_local_extrema = 'true' if use_local_extrema else 'false'
132+
self.gradient = gradient
127133

128134
# Time dimension settings.
129135
self.auto_play = 'true' if auto_play else 'false'

tests/plugins/test_heat_map_withtime.py

Lines changed: 45 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@
1616
import numpy as np
1717

1818

19+
def _normalize(rendered):
20+
return ''.join(rendered.split())
21+
22+
1923
def test_heat_map_with_time():
2024
np.random.seed(3141592)
2125
initial_data = (np.random.normal(size=(100, 2)) * np.array([[1, 1]]) +
@@ -41,45 +45,46 @@ def test_heat_map_with_time():
4145

4246
# We verify that the script part is correct.
4347
tmpl = Template("""
44-
var times = {{this.times}};
45-
46-
{{this._parent.get_name()}}.timeDimension = L.timeDimension(
47-
{times : times, currentTime: new Date(1)}
48-
);
49-
50-
var {{this._control_name}} = new L.Control.TimeDimensionCustom({{this.index}}, {
51-
autoPlay: {{this.auto_play}},
52-
backwardButton: {{this.backward_button}},
53-
displayDate: {{this.display_index}},
54-
forwardButton: {{this.forward_button}},
55-
limitMinimumRange: {{this.limit_minimum_range}},
56-
limitSliders: {{this.limit_sliders}},
57-
loopButton: {{this.loop_button}},
58-
maxSpeed: {{this.max_speed}},
59-
minSpeed: {{this.min_speed}},
60-
playButton: {{this.play_button}},
61-
playReverseButton: {{this.play_reverse_button}},
62-
position: "{{this.position}}",
63-
speedSlider: {{this.speed_slider}},
64-
speedStep: {{this.speed_step}},
65-
styleNS: "{{this.style_NS}}",
66-
timeSlider: {{this.time_slider}},
67-
timeSliderDrapUpdate: {{this.time_slider_drap_update}},
68-
timeSteps: {{this.index_steps}}
69-
})
70-
.addTo({{this._parent.get_name()}});
71-
72-
var {{this.get_name()}} = new TDHeatmap({{this.data}},
73-
{heatmapOptions: {
74-
radius: {{this.radius}},
75-
minOpacity: {{this.min_opacity}},
76-
maxOpacity: {{this.max_opacity}},
77-
scaleRadius: {{this.scale_radius}},
78-
useLocalExtrema: {{this.use_local_extrema}},
79-
defaultWeight: 1 ,
80-
}
81-
})
82-
.addTo({{this._parent.get_name()}});
48+
var times = {{this.times}};
49+
50+
{{this._parent.get_name()}}.timeDimension = L.timeDimension(
51+
{times : times, currentTime: new Date(1)}
52+
);
53+
54+
var {{this._control_name}} = new L.Control.TimeDimensionCustom({{this.index}}, {
55+
autoPlay: {{this.auto_play}},
56+
backwardButton: {{this.backward_button}},
57+
displayDate: {{this.display_index}},
58+
forwardButton: {{this.forward_button}},
59+
limitMinimumRange: {{this.limit_minimum_range}},
60+
limitSliders: {{this.limit_sliders}},
61+
loopButton: {{this.loop_button}},
62+
maxSpeed: {{this.max_speed}},
63+
minSpeed: {{this.min_speed}},
64+
playButton: {{this.play_button}},
65+
playReverseButton: {{this.play_reverse_button}},
66+
position: "{{this.position}}",
67+
speedSlider: {{this.speed_slider}},
68+
speedStep: {{this.speed_step}},
69+
styleNS: "{{this.style_NS}}",
70+
timeSlider: {{this.time_slider}},
71+
timeSliderDrapUpdate: {{this.time_slider_drap_update}},
72+
timeSteps: {{this.index_steps}}
73+
})
74+
.addTo({{this._parent.get_name()}});
75+
76+
var {{this.get_name()}} = new TDHeatmap({{this.data}},
77+
{heatmapOptions: {
78+
radius: {{this.radius}},
79+
minOpacity: {{this.min_opacity}},
80+
maxOpacity: {{this.max_opacity}},
81+
scaleRadius: {{this.scale_radius}},
82+
useLocalExtrema: {{this.use_local_extrema}},
83+
defaultWeight: 1,
84+
{% if this.gradient %}gradient: {{ this.gradient }}{% endif %}
85+
}
86+
})
87+
.addTo({{this._parent.get_name()}});
8388
""")
8489

85-
assert tmpl.render(this=hm)
90+
assert _normalize(tmpl.render(this=hm)) in _normalize(out)

0 commit comments

Comments
 (0)