-
-
Notifications
You must be signed in to change notification settings - Fork 826
Open
Labels
Description
What happened?
I wanted to create a visualization of a 2D grid, with an initial selection defined as a subgrid of the larger grid.
Here is a minimal reproducible example (y axis only):
import polars as pl
import altair as alt
# Create data
data = pl.DataFrame([
{"x": "1", "y": "A", "value": 1},
{"x": "2", "y": "A", "value": 2},
{"x": "3", "y": "A", "value": 3},
{"x": "1", "y": "B", "value": 4},
{"x": "2", "y": "B", "value": 5},
{"x": "3", "y": "B", "value": 6},
{"x": "1", "y": "C", "value": 7},
{"x": "2", "y": "C", "value": 8},
{"x": "3", "y": "C", "value": 9},
{"x": "1", "y": "D", "value": 10},
{"x": "2", "y": "D", "value": 11},
{"x": "3", "y": "D", "value": 12},
])
# Create interval selection with initial value
brush = alt.selection_interval(
encodings=['y'],
value={'y': ['A', 'B', 'C']}
)
# Create heatmap
chart = alt.Chart(data).mark_rect().encode(
x=alt.X('x:N'),
y=alt.Y('y:N'),
color=alt.Color('value:Q', scale=alt.Scale(scheme='blues')),
opacity=alt.condition(brush, alt.value(1), alt.value(0.3))
).add_params(
brush
).properties(
width=400,
height=300,
)
chart
But it does not work. I get
Multiple errors were found. Error 1: 'A' is an invalid value for `0`. Valid values are of type 'boolean', 'number', or 'object'. Error 2: 'B' is an invalid value for `1`. Valid values are of type 'boolean', 'number', or 'object'. Error 3: 'C' is an invalid value for `2`. Valid values are of type 'boolean', 'number', or 'object'.
At this point: I am confused for 2 reasons:
- the error message is not helpful at all to know why it fails (I tried to do it again with number instead of letters, and same error).
- Should the content of the list be the interval (start-end value) or the list of all selected values ? The documentation does not give any hint (https://altair-viz.github.io/user_guide/generated/api/altair.selection_interval.html)
What would you like to happen instead?
I was able to implement what I wanted in Vega-lite, and it actually works:
{
"$schema": "https://vega.github.io/schema/vega-lite/v5.json",
"width": 400,
"height": 300,
"data": {
"values": [
{"x": "1", "y": "1", "value": 1},
{"x": "2", "y": "1", "value": 2},
{"x": "3", "y": "1", "value": 3},
{"x": "1", "y": "2", "value": 4},
{"x": "2", "y": "2", "value": 5},
{"x": "3", "y": "2", "value": 6},
{"x": "1", "y": "3", "value": 7},
{"x": "2", "y": "3", "value": 8},
{"x": "3", "y": "3", "value": 9},
{"x": "1", "y": "4", "value": 10},
{"x": "2", "y": "4", "value": 11},
{"x": "3", "y": "4", "value": 12},
]
},
"params": [{
"name": "brush",
"select": {
"type": "interval",
"encodings": ["y"]
},
"value": {"y": ["1", "2", "3"]}
}],
"mark": "rect",
"encoding": {
"x": {"field": "x", "type": "ordinal"},
"y": {"field": "y", "type": "ordinal"},
"color": {
"field": "value",
"type": "quantitative",
"scale": {"scheme": "blues"}
},
"opacity": {
"condition": {"param": "brush", "value": 1},
"value": 0.3
}
}
}Altair should show exactly the same thing, without the weird error.
Note: I don't know if the specification somewhere, but there might be a mismatch between the vega-lite spec and the current way user understand the parameter (see this post as an example)
Which version of Altair are you using?
5.5.0
