13.visualization_maps_using_folium


复旦大学新闻学院新媒体硕士课程



使用folium做地图可视化



王成军

wangchengjun@nju.edu.cn

计算传播网 http://computational-communication.com


In [4]:
import folium, jinja2, vincent
from IPython.display import IFrame
from IPython.core.display import HTML
print(folium.__file__)
print(folium.__version__)


/Users/chengjun/anaconda/lib/python2.7/site-packages/folium-0.3.0.dev0-py2.7.egg/folium/__init__.pyc
0.3.0.dev

读入数据


In [76]:
import pickle

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

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

绘制地图


In [5]:
map_osm = folium.Map(location=[45.5236, -122.6750])
map_osm


Out[5]:

In [77]:
beijing = folium.Map(location=(39.90403,116.407526), zoom_start=10)

In [78]:
beijing


Out[78]:

MarkerCluster


In [64]:
from folium.plugins import MarkerCluster

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


Out[64]:

HeatMap


In [57]:
from folium import plugins

beijing.add_children(plugins.HeatMap(locations))


Out[57]:

In [ ]:
beijing.save('/Users/chengjun/GitHub/cjc2016/code/beijing_mobile.html')

PolyLine


In [65]:
colors = ['blue','orange',  'red']
for k, community in enumerate(edgeLoc):
    locations = edgeLoc[community]
    for loc in locations:
        line = folium.PolyLine(locations=loc, color=colors[k])
        beijing.add_children(line)

In [66]:
beijing


Out[66]: