使用folium做地图可视化

!pip install folium
Collecting folium
?25l  Downloading https://files.pythonhosted.org/packages/72/ff/004bfe344150a064e558cb2aedeaa02ecbf75e60e148a55a9198f0c41765/folium-0.10.0-py2.py3-none-any.whl (91kB)
    100% |████████████████████████████████| 92kB 72kB/s ta 0:00:0111
?25hCollecting branca>=0.3.0 (from folium)
  Using cached https://files.pythonhosted.org/packages/63/36/1c93318e9653f4e414a2e0c3b98fc898b4970e939afeedeee6075dd3b703/branca-0.3.1-py3-none-any.whl
Requirement already satisfied: numpy in /Users/datalab/anaconda3/lib/python3.7/site-packages (from folium) (1.16.2)
Requirement already satisfied: requests in /Users/datalab/anaconda3/lib/python3.7/site-packages (from folium) (2.21.0)
Requirement already satisfied: jinja2>=2.9 in /Users/datalab/anaconda3/lib/python3.7/site-packages (from folium) (2.10)
Requirement already satisfied: six in /Users/datalab/anaconda3/lib/python3.7/site-packages (from branca>=0.3.0->folium) (1.12.0)
Requirement already satisfied: certifi>=2017.4.17 in /Users/datalab/anaconda3/lib/python3.7/site-packages (from requests->folium) (2019.3.9)
Requirement already satisfied: chardet<3.1.0,>=3.0.2 in /Users/datalab/anaconda3/lib/python3.7/site-packages (from requests->folium) (3.0.4)
Requirement already satisfied: idna<2.9,>=2.5 in /Users/datalab/anaconda3/lib/python3.7/site-packages (from requests->folium) (2.8)
Requirement already satisfied: urllib3<1.25,>=1.21.1 in /Users/datalab/anaconda3/lib/python3.7/site-packages (from requests->folium) (1.24.1)
Requirement already satisfied: MarkupSafe>=0.23 in /Users/datalab/anaconda3/lib/python3.7/site-packages (from jinja2>=2.9->folium) (1.1.1)
Installing collected packages: branca, folium
Successfully installed branca-0.3.1 folium-0.10.0
!pip install vincent
Collecting vincent
  Using cached https://files.pythonhosted.org/packages/11/bf/a12ecaa21a2e376a16de67e09f64a38a4acd95e04e5dc35ad2f13a6f0bfd/vincent-0.4.4.tar.gz
Requirement already satisfied: pandas in /Users/datalab/anaconda3/lib/python3.7/site-packages (from vincent) (0.24.2)
Requirement already satisfied: pytz>=2011k in /Users/datalab/anaconda3/lib/python3.7/site-packages (from pandas->vincent) (2018.9)
Requirement already satisfied: python-dateutil>=2.5.0 in /Users/datalab/anaconda3/lib/python3.7/site-packages (from pandas->vincent) (2.8.0)
Requirement already satisfied: numpy>=1.12.0 in /Users/datalab/anaconda3/lib/python3.7/site-packages (from pandas->vincent) (1.16.2)
Requirement already satisfied: six>=1.5 in /Users/datalab/anaconda3/lib/python3.7/site-packages (from python-dateutil>=2.5.0->pandas->vincent) (1.12.0)
Building wheels for collected packages: vincent
  Building wheel for vincent (setup.py) ... ?25ldone
?25h  Stored in directory: /Users/datalab/Library/Caches/pip/wheels/4c/0d/8a/65f34c765c6094a71cce3e42a49a26533eef61695e9b505f03
Successfully built vincent
Installing collected packages: vincent
Successfully installed vincent-0.4.4
import folium, jinja2, vincent
from IPython.display import IFrame
from IPython.core.display import HTML
print(folium.__file__)
print(folium.__version__)
/Users/datalab/anaconda3/lib/python3.7/site-packages/folium/__init__.py
0.10.0

读入数据

import pickle

node_pkl = open('../data/nodeLoc.pkl', 'rb')
nodeLoc = pickle.load(node_pkl)
node_pkl.close()

edge_pkl = open('../data/edgeLoc.pkl', 'rb')
edgeLoc = pickle.load(edge_pkl)
edge_pkl.close()

绘制地图

map_osm = folium.Map(location=[45.5236, -122.6750])
map_osm
beijing = folium.Map(location=(39.90403,116.407526), zoom_start=10)
beijing

MarkerCluster

from folium.plugins import MarkerCluster

locations = nodeLoc
popups = ['{}'.format(loc) for loc in locations]
beijing.add_child(MarkerCluster(locations=locations, popups=popups))

HeatMap

from folium import plugins

beijing.add_child(plugins.HeatMap(locations))
/Users/datalab/anaconda3/lib/python3.7/site-packages/ipykernel_launcher.py:3: FutureWarning: Method `add_children` is deprecated. Please use `add_child` instead.
  This is separate from the ipykernel package so we can avoid doing imports until