-
Notifications
You must be signed in to change notification settings - Fork 794
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
Better access to logical operand predicates #695
Comments
I think using We have to do |
A question regarding the combination of multiple operands: can we do that in altair now? pop = vega_datasets.data.population()
alt.Chart(pop).mark_line().encode(
x='age:O',
y='sum(people):Q',
color='year:O'
).properties(
width=600, height=200
).transform_filter(
{'or': [alt.FieldRangePredicate(field='year', range=[1850, 1880]),
alt.FieldRangePredicate(field='year', range=[1960, 2000])],
# if I remove the following line and only keep 'or', it will work
'not': alt.FieldRangePredicate(field='year', range=[1870, 1880])
}
) |
Yes, you can do them in Altair. But no, you can't have Is this what you were intending to do?
|
Oh, I see... Yes this is exactly what I'm asking. Thanks! |
Do logical operands work within "alt.condition"? I have a bar chart with a selection_single, and a scatter plot with a selection_interval, and I'm trying to set opacity of the scatter to be conditional on both selections. I've tried the following and other variations but keep getting schema errors. The API says alt.condition should be able to take an operand, but I'm not sure about the syntax, which I adapted from the examples from transform_filter.
Here's a minimal example of what I'm trying to do. It works if I set the opacity condition to just a single selector.
|
For selections, logical operands like |
That did it, thanks! |
Sorry in advance if this is addressed by the above, but is it possible to combine a logical operand with a selection? I'd like to display all points not selected, like this:
|
Hi @breadbaron – I think this should work, but there is a bug in the way Here's a workaround until this is fixed: import numpy as np
data = pd.DataFrame( {col: np.random.randn(50) for col in 'xyz'})
selector = alt.selection_interval()
chart = alt.Chart( data )
xy = chart.mark_point().encode(
x = 'x',
y = 'y'
).add_selection( selector )
xz = chart.mark_point().encode(
x = 'x',
y = 'z'
).transform_filter( {'not': selector.ref()})
xy | xz |
@jakevdp thank you!! |
@breadbaron fixed in #1075, which will make it into the 2.2 release 😄 |
Features some specific cases from the user guide - https://altair-viz.github.io/user_guide/transform/filter.html#logical-operands - https://altair-viz.github.io/gallery/line_chart_with_cumsum_faceted.html #695
Vega-Lite has a number of logical operands; e.g.
alt.LogicalAndPredicate
alt.LogicalOrPredicate
alt.LogicalNotPredicate
These come up, for example, in constructing filter transforms. It would be useful to make these available to Altair users via Python's logical operators,
&
,|
, and~
, respectively.There are two challenges:
How to implement them effectively... maybe create some sort of
PredicateMixin
for these classes? If we were constructing the wrapper classes by hand, it would make sense foralt.Predicate
to be the base class of each of these logical predicates... perhaps we should change the code generation framework so that this kind of class hierarchy is (optionally) inferred in generated classes? Sounds hard to do well in a general way, though.The interaction with selection operands (e.g.
alt.SelectionOr
,alt.SelectionAnd
,alt.SelectionNot
, andalt.NamedSelection
) makes this a bit tricky, becauseNamedSelection
objects serve two purposes in Altair (they act both as aalt.SelectionDef
and aalt.SelectionPredicate
depending on the context).The text was updated successfully, but these errors were encountered: