Search code examples
pythonaltair

Altair Chart Interactions & Python


I'm working through the exercises in the Altair Tutorial and am struggling in the interactions section with making one of the points in the chart larger than the rest when I hover over it. I've tried a ton of different variations, the most recent being the below, but can't get past this. Any help would be appreciated.

selector = alt.selection_single(on='mouseover', nearest=True, empty="none")

alt.Chart(cars).mark_circle().encode(
    x='Horsepower:Q',
    y='Miles_per_Gallon:Q',
    color='Origin:N',
    size=alt.Size(selector, size=200, alt.Size=100)
).add_selection(selector)

Solution

  • You can use alt.condition together with alt.value as described here in the docs:

    import altair as alt
    from vega_datasets import data
    
    
    selector = alt.selection_single(on='mouseover', nearest=True, empty="none")
    
    alt.Chart(data.cars.url).mark_circle().encode(
        x='Horsepower:Q',
        y='Miles_per_Gallon:Q',
        color='Origin:N',
        size=alt.condition(selector, alt.value(200), alt.value(30))
    ).add_selection(
        selector
    )
    

    enter image description here

    (mouse pointer disappeared in screenshot)