簡介
keplergl
是由Uber開源的一款地理數(shù)據(jù)可視化工具,通過keplergl
我們可以在Jupyter notebook中使用臭挽,可視化效果如下圖所示:
安裝
官方文檔: https://docs.kepler.gl/docs/keplergl-jupyter
- 通過pip安裝
keplergl
:
pip install keplergl
- 如果你使用MAC通過PIP安裝而且notebook版本在5.3以上捂襟,可跳過此步:
jupyter nbextension install --py --sys-prefix keplergl # can be skipped for notebook 5.3 and above
jupyter nbextension enable --py --sys-prefix keplergl # can be skipped for notebook 5.3 and above
一個小例子
執(zhí)行以下代碼,會建立一個沒數(shù)據(jù)空窗口欢峰,出現(xiàn)如下效果葬荷,那就恭喜你安裝成功了~
from keplergl import KeplerGl
#創(chuàng)建一個KeplerGl對象
map_1 = KeplerGl(height=500)
#激活KeplerGl對象到j(luò)upyter的窗口中
map_1
添加數(shù)據(jù)
KeplerGl中支持三種格式的數(shù)據(jù),分別是csv
, GEOjson
和DataFrame
纽帖,下面以DataFrame
為例宠漩;
首先我們通過Pandas
讀取數(shù)據(jù):
import pandas as pd
df = pd.read_csv('rocket_launch_site_elevation_2019-10-27.csv')
df.head()
再新建一個KeplerGl,然后通過.add()
添加我們剛剛讀取的數(shù)據(jù)懊直;
在地圖上就能看到我們剛剛添加的數(shù)據(jù)了~
#創(chuàng)建一個KeplerGl對象
map_2 = KeplerGl(height=600)
#激活KeplerGl對象到j(luò)upyter的窗口中
map_2
# 添加數(shù)據(jù)
map_2.add_data(name='rocket', data=df)
map_2
定制圖表
與pyecharts
,matplotlib
等庫的配置方式不一樣扒吁,在Kepler
中你不需要寫很多關(guān)于顏色,圖表類型等等方面的配置代碼室囊,你只需要在窗口通過鼠標操作就好了雕崩,如下圖所示:
獲取配置
通過.config
獲取當(dāng)前圖表中的配置(包括你在窗口中的操作)~
map_2.config
當(dāng)然這個配置也可以復(fù)用的其他圖表上魁索,如下代碼~
#創(chuàng)建一個KeplerGl對象
# config讀取配置
map_3 = KeplerGl(height=600, config=map_2.config)
#激活KeplerGl對象到j(luò)upyter的窗口中
map_3
# 添加數(shù)據(jù)
map_3.add_data(name='rocket', data=df)
map_3
導(dǎo)出圖表
通過.save_to_html()
導(dǎo)出會生成一個html文件,支持參數(shù)如下:
data:圖表數(shù)據(jù)盼铁,如為空則會使用當(dāng)前圖表中的數(shù)據(jù)粗蔚;
config:圖表配置,如為空則會使用圖表中當(dāng)前的配置饶火;
file_name: 導(dǎo)出文件名鹏控,默認
keplergl_map.html
read_only:如為True,導(dǎo)出的文件將不支持配置肤寝;
map_3.save_to_html(file_name='kepler_example.html')
通過瀏覽器打開剛剛保存的文件当辐,就可以開始你的可視化之旅了~~
最后
簡單使用下來,KeplerGL是一款上手非常容易的可視化工具醒陆,不需要你去寫復(fù)雜的代碼完成圖表的配置瀑构,通過圖形化界面就能作出非常好看的效果裆针;
如果你工作中剛好需要用到地理數(shù)據(jù)的可視化刨摩,KeplerGL是一款非常不錯的選擇~