有没有办法动态更改 Altair 中选定点的 zorder?

Is there a way to dynamically change the zorder of selected points in Altair?

我有一个 Altair 图,其中多个点位于相似的 xy 位置,类似于 this plot in the Altair tutorial. Since I use tooltips, I'd like for the selected points to rise to the top (i.e. increase zorder in matplotlib 术语)。有没有办法做到这一点? (如果没有,我很乐意提交功能请求!)

不能,点的z-order是层内每个子图的顺序设置的,不能动态调整。这是 Vega-Lite 渲染模型的核心,不太可能更改。

不过,您可以使用一个技巧来实现您想要的效果,即叠加一个根据相关选择过滤的重复图表,使所选点显示在所有其他点之上。这是一个例子:

import altair as alt
from vega_datasets import data

cars = data.cars()

selection = alt.selection_single(on='mouseover', nearest=True)

base = alt.Chart(cars).mark_point().encode(
    x='Horsepower:Q',
    y='Miles_per_Gallon:Q',
    color=alt.condition(selection, 'Origin:N', alt.value('lightgray'))
)

alt.layer(
  base.add_selection(selection),
  base.transform_filter(selection).encode(tooltip='Name:N'),
)

Live Chart View