python - 带散点图的 Altair 链接地图
问题描述
我正在尝试创建一个类似于示例here和here的链接图。我想要一侧的散点图和另一侧的地理图。散点图中的点将在地图上相应的地理位置上显示为点。一旦我在散点图上选择了几个点,我只想在地图上看到这些点,反之亦然。然而,没能完成。
我认为问题是基数,或者这些图的 x 和 y 轴中使用的值。散点图的基础只使用值(数据框,选择了两个数字列),而 geomap 具有纬度和经度(topojson 文件,用于将点添加到地图上的纬度和经度列)。您可以将数据集视为来自 vegasets 的数据集:data.airports()
还有两个数字列。和 topojson 作为data.us_10m.url
有没有办法在它们之间建立联系?
解决方案
根据美国机场示例图并添加随附的散点图,您可以执行以下操作:
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)
请注意,地理投影目前不支持间隔选择,因此无法在地图本身上选择点。
推荐阅读
- scala - 如何使用 BehaviorTestKit 测试 MessageAdapter 效果
- regex - 如何改进姓氏的这个正则表达式?
- reactjs - 如何避免在下拉菜单 Semantic UI React 上叠加调光器?
- c# - 如何在代码中将 TextBoxes 绑定到 Properties.Settings.Default 中的每个属性?
- javascript - 过滤数组中的数据并返回调整后的数组
- python - 用满足 Column1 条件的字典键替换 Column2 值
- javascript - 过滤数组中的复杂数据并返回一个新数据
- git - 使用提交 SHA 查询带注释的标签
- php - 如何让我的代码只回显一次而不是两次?
- python-3.x - ANTLR4:在 python 的标签中使用 getText()