【foliumのPlugins】HeatMapを時間的変化で表すHeatMapWithTimeの使い方
今回は、地図作成ライブラリfoliumの数あるPluginsの中の1つHeatMapWithTimeをとりあえずどんな感じか使ってみる。
天気予報の雨雲レーダーのように、ヒートマップを時間的経過で動画的に表現する地図を作ることができます。
【実行環境】
- Android
- Termux
- Python3.9
- Jupyter Notebook
- 外部ライブラリ
- folium
目次
地図作成
時間経過の表示方法によって二種類の地図を作成する。
import numpy as np import folium from folium import plugins np.random.seed(0) # 座標[32, 131]にランダムな揺らぎを加える initial_data = np.random.normal(size=(15, 2)) * np.array([[1, 1]]) + np.array( [[32, 131]] ) print(initial_data.shape) initial_data
(15, 2)
array([[ 33.76405235, 131.40015721],
[ 32.97873798, 133.2408932 ],
[ 33.86755799, 130.02272212],
[ 32.95008842, 130.84864279],
[ 31.89678115, 131.4105985 ],
[ 32.14404357, 132.45427351],
[ 32.76103773, 131.12167502],
[ 32.44386323, 131.33367433],
[ 33.49407907, 130.79484174],
[ 32.3130677 , 130.14590426],
[ 29.44701018, 131.6536186 ],
[ 32.8644362 , 130.25783498],
[ 34.26975462, 129.54563433],
[ 32.04575852, 130.81281615],
[ 33.53277921, 132.46935877]])
# 移動する座標の差を生成 move_data = np.random.normal(size=(15, 2)) * 0.01 print(move_data.shape) move_data
(15, 2)
array([[ 0.00154947, 0.00378163],
[-0.00887786, -0.01980796],
[-0.00347912, 0.00156349],
[ 0.01230291, 0.0120238 ],
[-0.00387327, -0.00302303],
[-0.01048553, -0.01420018],
[-0.0170627 , 0.01950775],
[-0.00509652, -0.00438074],
[-0.01252795, 0.0077749 ],
[-0.01613898, -0.0021274 ],
[-0.00895467, 0.00386902],
[-0.00510805, -0.01180632],
[-0.00028182, 0.00428332],
[ 0.00066517, 0.00302472],
[-0.00634322, -0.00362741]])
# 移動データ data = [(initial_data + move_data * i).tolist() for i in range(5)] print(len(data)) data
5
[[[33.76405234596766, 131.40015720836723],
[32.97873798410574, 133.24089319920145],
[33.86755799014997, 130.02272212012357],
[32.95008841752559, 130.8486427917023],
[31.896781148206443, 131.41059850193838],
[32.14404357116088, 132.45427350696298],
[32.76103772514699, 131.12167501649282],
[32.44386323274543, 131.33367432737427],
[33.49407907315761, 130.7948417362342],
[32.3130677016509, 130.14590426069827],
[29.447010184165922, 131.65361859544035],
[32.86443619885951, 130.25783497959355],
[34.26975462398761, 129.54563432540124],
[32.045758517301444, 130.81281614997417],
[33.532779214358456, 132.46935876990028]],
[[33.76560182022463, 131.40393883356325],
[32.96986012662944, 133.22108523451922],
[33.864078868656705, 130.02428560981463],
[32.96239132433287, 130.86066659019014],
[31.892907880032364, 131.40757547443263],
[32.133558041510206, 132.4400733275912],
[32.74397502324074, 131.14118277044514],
[32.438766710927915, 131.32929358435817],
[33.48155111955711, 130.80261663979252],
[32.29692872317532, 130.14377685789614],
[29.438055518553984, 131.65748762041895],
[32.85932814748382, 130.24602865775233],
[34.269472801704225, 129.54991764410653],
[32.04642368952528, 130.81584086895157],
[33.52643599342164, 132.46573135824042]],
[[33.7671512944816, 131.4077204587593],
[32.96098226915314, 133.20127726983696],
[33.86059974716345, 130.02584909950565],
[32.97469423114015, 130.872690388678],
[31.889034611858285, 131.40455244692689],
[32.12307251185954, 132.4258731482194],
[32.72691232133449, 131.16069052439744],
[32.433670189110394, 131.32491284134204],
[33.46902316595661, 130.81039154335082],
[32.28078974469974, 130.14164945509398],
[29.42910085294205, 131.66135664539755],
[32.85422009610813, 130.2342223359111],
[34.269190979420834, 129.55420096281185],
[32.04708886174911, 130.81886558792897],
[33.52009277248484, 132.46210394658053]],
[[33.76870076873857, 131.4115020839553],
[32.952104411676835, 133.18146930515474],
[33.857120625670184, 130.0274125891967],
[32.98699713794743, 130.88471418716583],
[31.885161343684207, 131.4015294194211],
[32.11258698220887, 132.4116729688476],
[32.70984961942824, 131.18019827834976],
[32.42857366729288, 131.32053209832594],
[33.45649521235611, 130.81816644690915],
[32.264650766224165, 130.13952205229185],
[29.42014618733011, 131.66522567037612],
[32.84911204473244, 130.2224160140699],
[34.26890915713745, 129.55848428151714],
[32.047754033972936, 130.82189030690637],
[33.513749551548024, 132.45847653492066]],
[[33.77025024299554, 131.4152837091513],
[32.943226554200535, 133.1616613404725],
[33.85364150417692, 130.02897607888772],
[32.9993000447547, 130.8967379856537],
[31.881288075510124, 131.39850639191536],
[32.102101452558195, 132.3974727894758],
[32.69278691752199, 131.1997060323021],
[32.42347714547536, 131.3161513553098],
[33.44396725875561, 130.82594135046747],
[32.24851178774858, 130.13739464948972],
[29.411191521718177, 131.66909469535472],
[32.84400399335675, 130.21060969222864],
[34.26862733485406, 129.56276760022246],
[32.04841920619677, 130.82491502588377],
[33.50740633061122, 132.4548491232608]]]
# 座標にウェイトを追加する weight = 1 # default value for time_entry in data: for row in time_entry: row.append(weight) # 地図作成 1 m = folium.Map([32.0, 131.0], zoom_start=6) hm = plugins.HeatMapWithTime(data) hm.add_to(m) # 地図作成 2 # 日時で表示する from datetime import datetime, timedelta time_index = [ (datetime.now() + k * timedelta(1)).strftime("%Y-%m-%d") for k in range(len(data)) ] map = folium.Map([32.0, 131.0], zoom_start=6) hm2 = plugins.HeatMapWithTime(data, index=time_index, auto_play=True, max_opacity=0.3) hm2.add_to(map) # 二種類の地図作成 m.save("heatMapWithTime.html") map.save("heatMapWithTime2.html")
# 地図1 表示
m
Make this Notebook Trusted to load map: File -> Trust Notebook
# 地図2 表示 map
Make this Notebook Trusted to load map: File -> Trust Notebook
ヘルプ
# ヘルプの表示 help(folium.plugins.HeatMapWithTime) # または(Jupyterで #?folium.plugins.HeatMapWithTime
Help on class HeatMapWithTime in module folium.plugins.heat_map_withtime:
class HeatMapWithTime(folium.elements.JSCSSMixin, folium.map.Layer)
| HeatMapWithTime(data, index=None, name=None, radius=15, min_opacity=0, max_opacity=0.6, scale_radius=False, gradient=None, use_local_extrema=False, auto_play=False, display_index=True, index_steps=1, min_speed=0.1, max_speed=10, speed_step=0.1, position='bottomleft', overlay=True, control=True, show=True)
(以下略
参考リンク
おわりに
ここでは実験的に座標をNumpyでランダム生成している。座標数も15個と大幅に減らしてあるが、それでも生成された地図のコードは結構デカくなる。
実際的に使用する場合はなんらかの方法で収集した座標を用いると思いますが、座標やデータを集めるのが大変。毎度。
以上。