根据 Altair 中的选择突出显示地图区域

Highlighting map area based on selection in Altair

我想通过使用 selection_single 突出显示鼠标悬停(或最接近)的地图部分 - 就像工具提示的行为一样。但它总是固定在一个看似随机的点上。

例如,这里的县域应该高亮红色,当鼠标在它上面时。

import altair as alt
from vega_datasets import data

counties = alt.topo_feature(data.us_10m.url, 'counties')
source = data.unemployment.url

highlight = alt.selection_single(on='mouseover', nearest=True, fields=['id'], empty='none')

alt.Chart(counties).mark_geoshape().encode(
    color=alt.condition(highlight, alt.value('red'), 'rate:Q'),
    tooltip=['id:Q', 'rate:Q']
).transform_lookup(
    lookup='id',
    from_=alt.LookupData(source, 'id', ['rate'])
).add_selection(highlight).project(
    type='albersUsa'
).properties(
    width=900,
    height=600
)

但是无论我的鼠标移到哪里,高亮显示和工具提示都保持固定在 id = 22051(请看 Lousiana 的底部)。未添加选择时,工具提示工作正常,但添加选择后,甚至无法正常工作。

voronoi 镶嵌有帮助吗?像最近的线示例?我试过了,失败了,但我不确定我是否正确应用了它。

如果你删除 nearest=True:

import altair as alt
from vega_datasets import data

counties = alt.topo_feature(data.us_10m.url, 'counties')
source = data.unemployment.url

highlight = alt.selection_single(on='mouseover', fields=['id'], empty='none')

alt.Chart(counties).mark_geoshape().encode(
    color=alt.condition(highlight, alt.value('red'), 'rate:Q'),
    tooltip=['id:Q', 'rate:Q']
).transform_lookup(
    lookup='id',
    from_=alt.LookupData(source, 'id', ['rate'])
).add_selection(highlight).project(
    type='albersUsa'
).properties(
    width=900,
    height=600
)