Pydeck!空間數(shù)據(jù)可視化神器!
大家好,我是寶器。
今天給大家介紹一款超贊的空間(地理)數(shù)據(jù)可視化神器:Pydeck。
Pydeck庫(kù)通過deck.gl對(duì)數(shù)據(jù)進(jìn)行空間可視化渲染,對(duì)3D的可視化支持非常強(qiáng)。
使用文檔:
https://pydeck.gl/index.html
GitHub:
https://github.com/visgl/deck.gl/tree/master/bindings/pydeck

通過下面的命令進(jìn)行安裝。
#?安裝
pip?install?pydeck?-i?https://mirror.baidu.com/pypi/simple
可以在jupyter notebook及IDE上運(yùn)行,其中jupyter上需要安裝相關(guān)的插件。
#?啟用Pydeck
jupyter?nbextension?install?--sys-prefix?--symlink?--overwrite?--py?pydeck
jupyter?nbextension?enable?--sys-prefix?--py?pydeck
接下來,就給大家介紹一下相關(guān)的可視化案例。
使用的數(shù)據(jù)及代碼都已上傳,可在文末獲取~
①?弧形圖

打工人下班后的通勤情況,起點(diǎn)位于舊金山市中心(綠色),終點(diǎn)為目的地(紅色)。
數(shù)據(jù)由美國(guó)人口普查局收集。
地址:
https://lehd.ces.census.gov/data/
代碼如下。
import?pydeck?as?pdk
import?pandas?as?pd
DATA_URL?=?"https://raw.githubusercontent.com/ajduberstein/sf_public_data/master/bay_area_commute_routes.csv"
#?A?bounding?box?for?downtown?San?Francisco,?to?help?filter?this?commuter?data
DOWNTOWN_BOUNDING_BOX?=?[
????-122.43135291617365,
????37.766492914983864,
????-122.38706428091974,
????37.80583561830737,
]
def?in_bounding_box(point):
????"""Determine?whether?a?point?is?in?our?downtown?bounding?box"""
????lng,?lat?=?point
????in_lng_bounds?=?DOWNTOWN_BOUNDING_BOX[0]?<=?lng?<=?DOWNTOWN_BOUNDING_BOX[2]
????in_lat_bounds?=?DOWNTOWN_BOUNDING_BOX[1]?<=?lat?<=?DOWNTOWN_BOUNDING_BOX[3]
????return?in_lng_bounds?and?in_lat_bounds
df?=?pd.read_csv(DATA_URL)
#?Filter?to?bounding?box
df?=?df[df[["lng_w",?"lat_w"]].apply(lambda?row:?in_bounding_box(row),?axis=1)]
GREEN_RGB?=?[0,?255,?0,?40]
RED_RGB?=?[240,?100,?0,?40]
#?Specify?a?deck.gl?ArcLayer
arc_layer?=?pdk.Layer(
????"ArcLayer",
????data=df,
????get_width="S000?*?2",
????get_source_position=["lng_h",?"lat_h"],
????get_target_position=["lng_w",?"lat_w"],
????get_tilt=15,
????get_source_color=RED_RGB,
????get_target_color=GREEN_RGB,
????pickable=True,
????auto_highlight=True,
)
view_state?=?pdk.ViewState(latitude=37.7576171,?longitude=-122.5776844,?bearing=45,?pitch=50,?zoom=8,)
TOOLTIP_TEXT?=?{"html":?"{S000}?jobs?
?Home?of?commuter?in?red;?work?location?in?green"}
r?=?pdk.Deck(arc_layer,?initial_view_state=view_state,?tooltip=TOOLTIP_TEXT)
r.to_html("arc_layer.html")
②?3D柱狀圖

加拿大溫哥華的房?jī)r(jià)情況,輸入數(shù)據(jù)是GeoJSON格式的。
代碼如下。
import?pydeck?as?pdk
DATA_URL?=?"https://raw.githubusercontent.com/visgl/deck.gl-data/master/examples/geojson/vancouver-blocks.json"
LAND_COVER?=?[[[-123.0,?49.196],?[-123.0,?49.324],?[-123.306,?49.324],?[-123.306,?49.196]]]
INITIAL_VIEW_STATE?=?pdk.ViewState(latitude=49.254,?longitude=-123.13,?zoom=11,?max_zoom=16,?pitch=45,?bearing=0)
polygon?=?pdk.Layer(
????"PolygonLayer",
????LAND_COVER,
????stroked=False,
????#?processes?the?data?as?a?flat?longitude-latitude?pair
????get_polygon="-",
????get_fill_color=[0,?0,?0,?20],
)
geojson?=?pdk.Layer(
????"GeoJsonLayer",
????DATA_URL,
????opacity=0.8,
????stroked=False,
????filled=True,
????extruded=True,
????wireframe=True,
????get_elevation="properties.valuePerSqm?/?20",
????get_fill_color="[255,?255,?properties.growth?*?255]",
????get_line_color=[255,?255,?255],
)
r?=?pdk.Deck(layers=[polygon,?geojson],?initial_view_state=INITIAL_VIEW_STATE)
r.to_html("geojson_layer.html")
③?圖標(biāo)圖

德國(guó)啤酒花園的位置分布情況。
代碼如下。
import?pydeck?as?pdk
import?pandas?as?pd
#?Data?from?OpenStreetMap,?accessed?via?osmpy
DATA_URL?=?"https://raw.githubusercontent.com/ajduberstein/geo_datasets/master/biergartens.json"
ICON_URL?=?"https://upload.wikimedia.org/wikipedia/commons/c/c4/Projet_bi%C3%A8re_logo_v2.png"
icon_data?=?{
????#?Icon?from?Wikimedia,?used?the?Creative?Commons?Attribution-Share?Alike?3.0
????#?Unported,?2.5?Generic,?2.0?Generic?and?1.0?Generic?licenses
????"url":?ICON_URL,
????"width":?242,
????"height":?242,
????"anchorY":?242,
}
data?=?pd.read_json(DATA_URL)
data["icon_data"]?=?None
for?i?in?data.index:
????data["icon_data"][i]?=?icon_data
view_state?=?pdk.data_utils.compute_view(data[["lon",?"lat"]],?0.1)
icon_layer?=?pdk.Layer(
????type="IconLayer",
????data=data,
????get_icon="icon_data",
????get_size=4,
????size_scale=15,
????get_position=["lon",?"lat"],
????pickable=True,
)
r?=?pdk.Deck(layers=[icon_layer],?initial_view_state=view_state,?tooltip={"text":?"{tags}"})
r.to_html("icon_layer.html")
④?流向圖

從希思羅機(jī)場(chǎng)飛往歐洲各地的航班路線圖。
代碼如下。
import?pydeck?as?pdk
DATA_URL?=?{
????"AIRPORTS":?"https://raw.githubusercontent.com/visgl/deck.gl-data/master/examples/line/airports.json",
????"FLIGHT_PATHS":?"https://raw.githubusercontent.com/visgl/deck.gl-data/master/examples/line/heathrow-flights.json",??#?noqa
}
INITIAL_VIEW_STATE?=?pdk.ViewState(latitude=47.65,?longitude=7,?zoom=4.5,?max_zoom=16,?pitch=50,?bearing=0)
#?RGBA?value?generated?in?Javascript?by?deck.gl's?Javascript?expression?parser
GET_COLOR_JS?=?[
????"255?*?(1?-?(start[2]?/?10000)?*?2)",
????"128?*?(start[2]?/?10000)",
????"255?*?(start[2]?/?10000)",
????"255?*?(1?-?(start[2]?/?10000))",
]
scatterplot?=?pdk.Layer(
????"ScatterplotLayer",
????DATA_URL["AIRPORTS"],
????radius_scale=20,
????get_position="coordinates",
????get_fill_color=[255,?140,?0],
????get_radius=60,
????pickable=True,
)
line_layer?=?pdk.Layer(
????"LineLayer",
????DATA_URL["FLIGHT_PATHS"],
????get_source_position="start",
????get_target_position="end",
????get_color=GET_COLOR_JS,
????get_width=10,
????highlight_color=[255,?255,?0],
????picking_radius=10,
????auto_highlight=True,
????pickable=True,
)
layers?=?[scatterplot,?line_layer]
r?=?pdk.Deck(layers=layers,?initial_view_state=INITIAL_VIEW_STATE)
r.to_html("line_layer.html")
⑤?路徑圖

灣區(qū)快速交通線路圖。
代碼如下。
import?pandas?as?pd
import?pydeck?as?pdk
DATA_URL?=?"https://raw.githubusercontent.com/visgl/deck.gl-data/master/website/bart-lines.json"
df?=?pd.read_json(DATA_URL)
def?hex_to_rgb(h):
????h?=?h.lstrip("#")
????return?tuple(int(h[i?:?i?+?2],?16)?for?i?in?(0,?2,?4))
df["color"]?=?df["color"].apply(hex_to_rgb)
view_state?=?pdk.ViewState(latitude=37.782556,?longitude=-122.3484867,?zoom=10)
layer?=?pdk.Layer(
????type="PathLayer",
????data=df,
????pickable=True,
????get_color="color",
????width_scale=20,
????width_min_pixels=2,
????get_path="path",
????get_width=5,
)
r?=?pdk.Deck(layers=[layer],?initial_view_state=view_state,?tooltip={"text":?"{name}"})
r.to_html("path_layer.html")
⑥?散點(diǎn)圖

舊金山市內(nèi)各地鐵站的出口數(shù)量圖。
代碼如下。
import?pydeck?as?pdk
import?pandas?as?pd
import?math
SCATTERPLOT_LAYER_DATA?=?"https://raw.githubusercontent.com/visgl/deck.gl-data/master/website/bart-stations.json"
df?=?pd.read_json(SCATTERPLOT_LAYER_DATA)
#?Use?pandas?to?calculate?additional?data
df["exits_radius"]?=?df["exits"].apply(lambda?exits_count:?math.sqrt(exits_count))
#?Define?a?layer?to?display?on?a?map
layer?=?pdk.Layer(
????"ScatterplotLayer",
????df,
????pickable=True,
????opacity=0.8,
????stroked=True,
????filled=True,
????radius_scale=6,
????radius_min_pixels=1,
????radius_max_pixels=100,
????line_width_min_pixels=1,
????get_position="coordinates",
????get_radius="exits_radius",
????get_fill_color=[255,?140,?0],
????get_line_color=[0,?0,?0],
)
#?Set?the?viewport?location
view_state?=?pdk.ViewState(latitude=37.7749295,?longitude=-122.4194155,?zoom=10,?bearing=0,?pitch=0)
#?Render
r?=?pdk.Deck(layers=[layer],?initial_view_state=view_state,?tooltip={"text":?"{name}\n{address}"})
r.to_html("scatterplot_layer.html")
⑦?其他
案例較多,這里就不貼代碼,直接展示啦!


1906年,Britton&Rey繪制的舊金山1906年火災(zāi)地圖,覆蓋在交互式的舊金山地圖上。

臺(tái)北房?jī)r(jià)。數(shù)據(jù)為2012-2013年。柱子的高度表示單位面積價(jià)格的上升,顏色表示離地鐵站的距離。

2006年美國(guó)新墨西哥州飼養(yǎng)的牲畜的位置。
家禽的位置是藍(lán)色的,牛的位置是橙色的。與Mapbox上的衛(wèi)星圖像疊加以突出地形如何影響農(nóng)業(yè)。

通用樣式,一幅世界地圖。

通過geopandas的數(shù)據(jù),繪制世界地圖。
其中g(shù)eopandas安裝起來有點(diǎn)費(fèi)勁,建議使用whl安裝包。

世界上超過33000個(gè)發(fā)電廠按照它們的生產(chǎn)能力(以高度表示)和燃料類型(綠色,如果可再生的話)繪制在一個(gè)球形地圖上。

舊金山國(guó)際機(jī)場(chǎng)直飛航線圖。起點(diǎn)是綠色的,目的地是藍(lán)色的。

舊金山自行車停車地點(diǎn)的分布情況。


網(wǎng)格地圖。

2006年在美國(guó)新墨西哥州飼養(yǎng)牲畜的位置熱力圖。
家禽的位置是藍(lán)色的,牛的位置是橙色的。與Mapbox上的衛(wèi)星圖像疊加以突出地形如何影響農(nóng)業(yè)。

英國(guó)從1979年起發(fā)生的人身傷害交通事故。

點(diǎn)云圖,激光掃描,由無數(shù)個(gè)點(diǎn)組成。

加拿大溫哥華的房?jī)r(jià)情況。

這個(gè)不知道是啥玩意...

在舊金山內(nèi)的自行車停車位,將數(shù)據(jù)聚合網(wǎng)格。

使用AWS開放數(shù)據(jù)和Mapbox衛(wèi)星圖像制作一個(gè)地形圖。

舊金山市內(nèi)各種公共交通站點(diǎn)的名稱,在站點(diǎn)的位置上標(biāo)出。

一輛共享單車在舊金山的騎行情況,從開始到逐漸消失。
好了,以上就是今天分享的內(nèi)容,大家可以自行去動(dòng)手練習(xí)。
這里小F用到了Mapbox地圖,是需要自行去注冊(cè),獲取token。
地址:
https://account.mapbox.com/

然后在pydeck的deck方法中,將token添加到api_keys參數(shù)即可。

相關(guān)的文件及代碼都已上傳,獲取方式:
鏈接:
https://pan.baidu.com/s/1sK4ZgMKNZ1upb-P6bzGbWg?
提取碼:mzca?
喜歡文章的話,幫忙點(diǎn)個(gè)贊唄

推薦閱讀
歡迎長(zhǎng)按掃碼關(guān)注「數(shù)據(jù)管道」
