[Python] How to link selection across marks/symbols in Altair?

Discussão em 'Python' iniciado por Stack, Abril 25, 2025.

  1. Stack

    Stack Membro Participativo

    Imagine we have an Altair graph in Python like:

    import altair as alt
    from vega_datasets import data

    cars = data.cars.url

    selector = alt.selection_point()
    conditional = alt.when(selector).then("Origin:N").otherwise(alt.value("lightgray"))

    base = alt.Chart(cars).encode(
    x="Horsepower:Q",
    y="Miles_per_Gallon:Q",
    )

    points = base.mark_point().encode(color=conditional).add_params(selector)
    lines = base.mark_line().encode(color=conditional)

    (points + lines).properties(width=600, height=300).interactive()


    which gives the graph:

    [​IMG]

    Currently, when selecting the points, all other points and all the lines go gray. But, is there a way to ensure that the selected point and the whole connecting line and points are both highlighted too?

    I've been scouring the docs on this for a while and haven't quite worked it out. All my attempts have kept the selection separate, not shared across the different marks/symbols. I'm looking for some concept of the selector being 'shared' across the different layers.

    Continue reading...

Compartilhe esta Página