今天给大家介绍一款超赞的空间(地理)数据可视化神器:Pydeck。
Pydeck库通过deck.gl对数据进行空间可视化渲染,对3D的可视化支持非常强。
使用文档:
https://pydeck.gl/index.html
GitHub:
https://github.com/visgl/deck.gl/tree/master/bindings/pydeck
通过下面的命令进行安装。
# 安装
pip install pydeck -i https://mirror.baidu.com/pypi/simple
可以在jupyter notebook及IDE上运行,其中jupyter上需要安装相关的插件。
# 启用Pydeck
jupyter nbextension install --sys-prefix --symlink --overwrite --py pydeck
jupyter nbextension enable --sys-prefix --py pydeck
接下来,就给大家介绍一下相关的可视化案例。
使用的数据及代码都已上传,可在文末获取~
01
弧形图
打工人下班后的通勤情况,起点位于旧金山市中心(绿色),终点为目的地(红色)。
数据由美国人口普查局收集。
地址:
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 <br /> Home of commuter in red; work location in green"}
r = pdk.Deck(arc_layer