Only show intersection of 2 selectors when both active in altair chart

Viewed 121

I have made an Altair Visualization that contains 2 selectors on two different variables of my dataframe.

When I try to use one it works well -> https://imgur.com/gallery/bDfRV9q

When I use the other selector, it works also fine -> https://imgur.com/gallery/ycCMy20

However when I use both selectors I would like to only show the intersection of both selectors and not what is shown here -> https://imgur.com/gallery/lfcqcY9

Is it possible, to display the selection of each selector when only one of them is active and the intersection when both are active?

Edit: I add the code to this problem with sample data so hopefully its easier to reproduce.

import altair as alt
from vega_datasets import data

cars = data.cars.url

alex = alt.selection_interval(
    on="[mousedown[event.altKey], mouseup] > mousemove",
    name='alex', resolve='intersect'
)
morgan = alt.selection_interval(
    on="[mousedown[event.shiftKey], mouseup] > mousemove",
    mark=alt.BrushConfig(fill="#fdbb84", fillOpacity=0.5, stroke="#e34a33"),
    name='morgan', resolve='intersect'
)

alt.Chart(cars).mark_rect().encode(
    x='Cylinders:O',
    y='Origin:O',
    color=alt.condition((alex & ~morgan) | (~alex & morgan) | (alex & morgan), 'count()', alt.ColorValue("grey"))
).add_selection(
    alex, morgan
).properties(
    width=300,
    height=180
)

I would like the chart to show the selection of "alex" when only the "alex" selector is chosen, "morgan" when only "morgan" is chosen and only the intersection, when both selectors "alex" and "morgan" are chosen.

Thank you so much in advanced!

0 Answers
Related