首页 > 解决方案 > 带散点图的 Altair 链接地图

问题描述

我正在尝试创建一个类似于示例herehere的链接图。我想要一侧的散点图和另一侧的地理图。散点图中的点将在地图上相应的地理位置上显示为点。一旦我在散点图上选择了几个点,我只想在地图上看到这些点,反之亦然。然而,没能完成。

我认为问题是基数,或者这些图的 x 和 y 轴中使用的值。散点图的基础只使用值(数据框,选择了两个数字列),而 geomap 具有纬度和经度(topojson 文件,用于将点添加到地图上的纬度和经度列)。您可以将数据集视为来自 vegasets 的数据集:data.airports()还有两个数字列。和 topojson 作为data.us_10m.url

有没有办法在它们之间建立联系?

标签: pythonjupyter-labaltair

解决方案


根据美国机场示例图并添加随附的散点图,您可以执行以下操作:

import altair as alt
from vega_datasets import data

airports = data.airports()
states = alt.topo_feature(data.us_10m.url, feature='states')
selection = alt.selection_interval()

# US states background
background = alt.Chart(states).mark_geoshape(
    fill='lightgray',
    stroke='white'
).properties(
    width=500,
    height=300
).project('albersUsa')

# airport positions on background
points = alt.Chart(airports).mark_circle(
    size=10,
).encode(
    longitude='longitude:Q',
    latitude='latitude:Q',
    tooltip=['name', 'city', 'state'],
    color=alt.condition(selection, alt.value('steelblue'), alt.value('lightgray'))
)

#lat/lon scatter
scatter = alt.Chart(airports).mark_point().encode(
    x='longitude:Q',
    y='latitude:Q',
    color=alt.condition(selection, alt.value('steelblue'), alt.value('lightgray'))   
).add_selection(
    selection
)

scatter | (background + points)

在此处输入图像描述

请注意,地理投影目前不支持间隔选择,因此无法在地图本身上选择点。


推荐阅读