大家好,我是林路~今天給大家介紹一個超棒的空間(地理)資料視覺化神器:keplergl。小編最近偶然發現的這個神器是Uber完全開源的,也是Uber內部進行空間資料視覺化的預設工具。
透過其面向Python開放的介面包keplergl,我們可以在jupyter notebook中透過書寫Python程式碼的方式傳入多種格式的資料,在其嵌入notebook的互動視窗中使用其內建的多種豐富的空間資料視覺化功能。下面有3個主要的學習地址:
1、官網地址:https://kepler。gl/
2、jupyter notebook使用手冊地址:https://github。com/keplergl/kepler。gl/tree/master/docs/keplergl-jupyter#geojson
3、案例地址:https://github。com/keplergl/kepler。gl/tree/master/bindings/kepler。gl-jupyter/notebooks
安裝
keplergl的安裝是非常簡單的。如果報錯,請自行百度或者谷歌解決,小編是一次性裝成功的❤️
pip install keplergl
驚豔圖
一大波驚豔的圖形來了:
案例入門
import pandas as pdimport geopandas as gpdfrom keplergl import KeplerGl# 建立物件kep1 = KeplerGl(height=600)# 啟用物件,載入到jupyter notebook中kep1
可以看到,運行了基本程式碼後在Jupyter中直接就產生了內建的圖形,圖形本身也是動態的;暗黑色的背景也是小編喜歡的:
新增資料
keplergl預設是可以新增3種形式的資料:
csv
GeoJSON
DataFrame
csv格式
本地目錄下有一份csv資料:china。csv,記錄的是我國各個省份的經緯度:
with open(“china。csv”,“r”) as f: csv_data = f。read() # add_data新增資料kep1。add_data(data=csv_data, name=“csv_kep”)kep1
DataFrame格式
china = pd。read_csv(“china。csv”)kep1。add_data(data=china, name=“dataframe_kep”)kep1
GeoJson格式
url = ‘http://eric。clst。org/assets/wiki/uploads/Stuff/gz_2010_us_040_00_500k。json’country_gdf = gpd。read_file(url) # geopandas讀取json檔案kep1。add_data(data=country_gdf, name=“state”)kep1
自定義圖形
Keplergl的自定義方法:
關鍵性按鈕
。進去了之後就可以自定義操作
配置的儲存及複用
例項化kep的配置可以儲存在後面的例項物件中複用:
1、儲存:
# 儲存成檔案with open(‘config1。py’,‘w’) as f: f。write(‘config={}’。format(kep1。config)) # 執行:魔法命令%run%run config1。py
2、複用
kep2 = KeplerGl(height=400, data={“layer1”:df}, config=kep1。config # kep1的配置 )kep2
儲存圖形
1、簡約版本,主要是檔名
kep1。save_to_html(file_name=“first_kep。html”)
2、完整版本:檔名、配置、資料、可讀性
# 4個引數kep1。save_to_html(file_name=“first_kep。html”, data={‘data_1’:china}, config=config, read_only=True )
線上操作
上面顯示的操作都是在notebook中完成的,我們也可以直接線上操作:https://kepler。gl/demo
等認真研究過這個工具之後再分享更多的文章,這個庫值得好好研究
原文連結:https://mp。weixin。qq。com/s/gI9tjY647wSA2KRWleoCuw