Search code examples
altair

Is there a way to customize the colors of a selection detail chart in Altair?


I have been playing around with the following selection detail example from the Altair gallery, as I think I would like to do something similar with some data I have:

https://altair-viz.github.io/gallery/select_detail.html

However, I am wondering if there is a way to make it so that all the points and lines are a uniform color, because the redundant encoding feels confusing to me. So far, I have not been able to come up with a way to do this. I can change the color scale using alt.Color, but I am unsure how to specify there to be just one color total.


Solution

  • You can use the detail encoding to group observations without assigning them a visual attribute:

    points = base.mark_point(filled=True, size=200).encode(
        x='mean(x)',
        y='mean(y)',
        detail='id:O',
        opacity=alt.condition(selector, alt.value(1), alt.value(0.2)),
    )
    
    timeseries = base.mark_line().encode(
        x='time',
        y=alt.Y('value', scale=alt.Scale(domain=(-15, 15))),
        detail='id:O',
    ).transform_filter(
        selector
    )
    
    points | timeseries
    

    enter image description here