简体   繁体   English

带散点图的Altair链接地图

[英]Altair linked map with scatter plot

I am trying to create a linked plot similar to examples here and here . 我正在尝试创建一个类似于此处此处的示例的链接图。 I want a scatter plot on one side and a geomap on the other. 我要在一侧绘制散点图,在另一侧绘制地理图。 The dots in the scatter plot will show up as dots on their corresponding geolocations on the map. 散点图中的点将在地图上相应的地理位置上显示为点。 Once I select a few points on the scatter plot, I'd like to see only those points on the map, or vice versa. 在散点图上选择了几个点后,我只想在地图上看到这些点,反之亦然。 However, couldn't get it done. 但是,无法完成。

I think the problem is the base, or the values used in their x and y axes of these plots. 我认为问题是基础,或者这些图的x和y轴中使用的值。 The scatter plot's base uses just values (the dataframe, two numeric columns selected), while geomap has lat and long (the topojson file, the latitude and longitude columns used for adding the points onto the map). 散点图的基础仅使用值(数据框,已选择两个数字列),而地理地图具有经度和纬度(topojson文件,用于将点添加到地图上的纬度和经度列)。 You can think of the dataset as the one from vegasets: data.airports() with two more numeric columns. 您可以将数据集视为来自vegasets的数据集: data.airports()具有另外两个数字列。 And the topojson as data.us_10m.url 和topojson作为data.us_10m.url

Is there a way to establish a connection between them? 有没有办法在他们之间建立联系?

Working from the US Airports example plot and adding an accompanying scatter plot, you can do something like this: 从“ 美国机场”示例图进行工作并添加伴随的散点图,您可以执行以下操作:

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)

在此处输入图片说明

Note that interval selections are currently not supported on geographic projections, so it will not be possible to select points on the map itself. 请注意,地理投影目前不支持区间选择,因此将无法在地图本身上选择点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM