Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Composing Multiple Selections #1704

Closed
mattijn opened this issue Sep 25, 2019 · 5 comments
Closed

Composing Multiple Selections #1704

mattijn opened this issue Sep 25, 2019 · 5 comments
Labels

Comments

@mattijn
Copy link
Contributor

mattijn commented Sep 25, 2019

What is the best approach for combining multiple selections in Altair?
In Vega-Lite this is described as such for a combined conditioned color selection:

"color": {
  "condition": {
    "selection": {"and": ["alex", "morgan"]},
    "aggregate": "count", "type": "quantitative"
  },
  "value": "grey"
}

And I can create a working example similar to the Vega-Lite docs as such in Altair:

import altair as alt
from vega_datasets import data

source = data.cars()

alex = alt.selection_interval(
    on="[mousedown[!event.shiftKey], mouseup] > mousemove",
    translate="[mousedown[!event.shiftKey], mouseup] > mousemove", 
    name='alex'
)
morgan = alt.selection_interval(
    on="[mousedown[event.shiftKey], mouseup] > mousemove",
    translate="[mousedown[event.shiftKey], mouseup] > mousemove",
    mark={"fill": "#fdbb84", "fillOpacity": 0.5, "stroke": "#e34a33"},
    name='morgan'
)

alt.Chart(source).mark_rect().encode(
    x='Cylinders:O',
    y='Origin:O',
    color=alt.condition({"selection": {"and": [alex.name, morgan.name]}}, 'count()', alt.ColorValue("grey"))    
).add_selection(
    alex, morgan
)

image

But it doesn't feel very pythonic.
I saw there is an alt.SelectionAnd() function, but I don't know how it should work. I tried alt.SelectionAnd([alex.name, morgan.name]), but this returns:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-55-409c05815fbc> in <module>
----> 1 alt.SelectionAnd([alex.name, morgan.name])

TypeError: __init__() takes 1 positional argument but 2 were given

Some help, advice or insight is appreciated.

(I'm also not sure if the on and translate within the alt.selection_interval() function should be like this in Altair)

@jakevdp
Copy link
Collaborator

jakevdp commented Sep 25, 2019

Bitwise operators are supported for selections: e.g. alt.condition(alex & morgan, ...) or alt.condition(alex | morgan, ...)

@jakevdp
Copy link
Collaborator

jakevdp commented Sep 25, 2019

alt.SelectionAnd is a low-level wrapper around the SelectionAnd definition in the vega-lite schema. If you look at the return value of alex & morgan, you'll see that it is a properly constructed SelectionAnd object.

@mattijn
Copy link
Contributor Author

mattijn commented Sep 25, 2019

Wow! Thats neat! Hidden beauty!

@mattijn mattijn closed this as completed Sep 25, 2019
@jakevdp
Copy link
Collaborator

jakevdp commented Sep 25, 2019

Great! Is there any place in the docs we could add this that you might have found before asking the question?

@mattijn
Copy link
Contributor Author

mattijn commented Sep 25, 2019

I guess here: https://altair-viz.github.io/user_guide/interactions.html#selection-types-interval-single-multi.
I should be able to add this example as a PR later this week, but this part:

morgan = alt.selection_interval(
    on="[mousedown[event.shiftKey], mouseup] > mousemove",
    translate="[mousedown[event.shiftKey], mouseup] > mousemove",
    mark={"fill": "#fdbb84", "fillOpacity": 0.5, "stroke": "#e34a33"},
    name='morgan'
)

is a bit verbose.
But we can discuss this in that PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants