Search code examples
filterhoverdata-visualizationvisualizationaltair

filter chart based on hovering over values in altair


I'm trying to filter my data using Mouse Hover.

so far i can hover over them but honestly don't know how when i hover over a value that belongs to 1st series all values in that Series would show up and the rest would be grey.

here's my code:

import pandas as pd
import altair as alt
from vega_datasets import data
anscombe = data.anscombe()

    single_nearest = alt.selection_single(on="mouseover",nearest=False,clear='mouseout')

alt.Chart(anscombe).mark_circle(size=100).encode(
        alt.X('X'),
        alt.Y('Y'),
        color=alt.condition(single_nearest, 'Series', alt.value('lightgray'))
        #tooltip= ['X', 'Y']
    ).transform_filter({'field': 'Series', 'oneOf': ['I', 'II','III','IV']}).add_selection(single_nearest)#.interactive()

it looks like this

enter image description here

to sum up when i hover over Orange( the second Series), only orange (Second Series) would show as Orange and others will be geryed out.


Solution

  • i had to use Multi select instead of single select

    single_nearest = alt.selection_multi(on="mouseover",nearest=False,clear='mouseout',fields=['Series'])
    
    alt.Chart(anscombe).mark_circle(size=100).encode(
            alt.X('X'),
            alt.Y('Y'),
            color=alt.condition(single_nearest, 'Series', alt.value('lightgray'))
            #tooltip= ['X', 'Y']
        ).transform_filter({'field': 'Series', 'oneOf': ['I', 'II','III','IV']}).add_selection(single_nearest)#.interactive()