Skip to content

value of selection_interval should be list, not tuple #3896

@rambip

Description

@rambip

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:

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
        }
      }
}

Output:
Image

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugneeds-triageBug report needs maintainer response

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions