如何利用 folium 软件包绘制 Google 地图?


Folium 是一个非常强大的 Python 库,可让你创建多种小册子地图。由于小册子/folium 地图是交互式的,因此它们非常适合制作仪表板。

安装

使用 pip 安装 folium 非常容易−

$pip install folium


就像你从下面的屏幕截图中看到的,你只需要在控制台/cmd 中输入上面的命令,pip 就会安装 folium 以及 Python 安装所需的依赖项。


基本地图

#Import library
import folium

#Uses lat then lon. & zoomlevel 4.The bigger the zoom number, the closer in you get.
mapOBJ = folium.Map(location=[17.3616, 78.4747], zoom_start = 4, tiles = 'Stamen Terrain')

# save method of Map object, will create a map

mapOBJ.save('map2.html')

输出

案例 2:GeoJson

import folium
import os
from folium import features
import numpy as np
import pandas as pd

N = 1000

lons = +5 - np.random.normal(size=N)
lats = 48 - np.random.normal(size=N)

data = {
   'type': 'FeatureCollection',
   'features': [
      {
         'type': 'Feature',
         'geometry': {
            'type': 'MultiPoint',
            'coordinates': [[lon, lat] for (lat, lon) in zip(lats, lons)],
         },
         'properties': {'prop0': 'value0'}
      },
   ],
}
m = folium.Map([17.3616, 78.4747], zoom_start = 10)
m.add_child(features.GeoJson(data))
m.save('Features.html')

输出

案例 3:标记、图标、弹出窗口

import folium
from folium import features

m = folium.Map([17.3616, 78.4747], zoom_start = 15)
mk = features.Marker([17.3616, 78.4747])
pp = folium.Popup('Charminar')
ic = features.Icon(color='red')

mk.add_child(ic)
mk.add_child(pp)
m.add_child(mk)

m.save('Features_1.html')

输出

案例 4

我使用了一个新的库 - vincent,Vincent 获取 Python 数据结构(元组、列表、字典和 Pandas 数据帧)并将它们转换为 Vega 可视化语法。

使用 pip,安装 vincent 非常容易。

$pip install vincent
C:\WINDOWS\system32>pip install vincent
Collecting vincent
Downloading https://files.pythonhosted.org/packages/11/bf/a12ecaa21a2e376a16de67e09f64a38a4acd95e04e5dc35ad2f13a6f0bfd/vincent-0.4.4.tar.gz
Requirement already satisfied: pandas in c:\python\python361\lib\site-packages (from vincent) (0.20.2)
Requirement already satisfied: pytz >= 2011k in c:\python\python361\lib\site-packages (from pandas -> vincent) (2017.2)
Requirement already satisfied: python-dateutil >= 2 in c:\python\python361\lib\site-packages (from pandas -> vincent) (2.6.0)
Requirement already satisfied: numpy >= 1.7.0 in c:\python\python361\lib\site-packages (from pandas -> vincent) (1.16.0)
Requirement already satisfied: six >= 1.5 in c:\python\python361\lib\site-packages (from python-dateutil >= 2 -> pandas -> vincent) (1.10.0)
Building wheels for collected packages: vincent
Running setup.py bdist_wheel for vincent ... done
Stored in directory: C:\Users\rajesh\AppData\Local\pip\Cache\wheels\4c\0d\8a\65f34c765c6094a71cce3e42a49a26533eef61695e9b505f03
Successfully built vincent
Installing collected packages: vincent
Successfully installed vincent-0.4.4


import folium
from folium import features
import numpy as np
import json
import vincent

N = 100

multi_iter2 = {
   'x': np.random.uniform(size=(N,)),
   'y': np.random.uniform(size=(N,)),
}

scatter = vincent.Scatter(multi_iter2, iter_idx='x', height=100, width=200)
data = json.loads(scatter.to_json())

m = folium.Map([17.3616, 78.4747], zoom_start=10)
mk = features.Marker([17.3616, 78.4747])
p = folium.Popup('Charminar')
v = features.Vega(data, width='100%', height='100%')

mk.add_child(p)
p.add_child(v)
m.add_child(mk)

m.save('Features_2.html')

输出

更新日期:2019-7-30

1 千+ 次查看

开启你的 职业

通过完成课程获得认证

开始
广告