带散点图的Altair链接地图

时间:2019-06-03 13:15:35

标签: python jupyter-lab altair

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

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

是否可以在它们之间建立连接?

1 个答案:

答案 0 :(得分:2)

US Airports示例图开始,并添加一个伴随的散点图,您可以执行以下操作:

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)

enter image description here

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