question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Selection Multi Init

See original GitHub issue

Is there public documentation for how to pass initial values to selection multi. For example in this chart from the documentation how can I make the initial selection Japan and USA. Is there a way to pass init to alt.selection_multi to make this happen?

selection = alt.selection_multi(fields=['Origin'])
color = alt.condition(selection,
                      alt.Color('Origin:N', legend=None),
                      alt.value('lightgray'))

scatter = alt.Chart(cars).mark_point().encode(
    x='Horsepower:Q',
    y='Miles_per_Gallon:Q',
    color=color,
    tooltip='Name:N'
)

legend = alt.Chart(cars).mark_point().encode(
    y=alt.Y('Origin:N', axis=alt.Axis(orient='right')),
    color=color
).add_selection(
    selection
)

scatter | legend

I’ve tried doing something like the following but this appears to have no effect.

import altair as alt
from vega_datasets import data

cars = data.cars.url

selection = alt.selection_multi(
    fields=['Origin'],
    init=[{'Origin': 'USA'}, {'Origin': 'Japan'}]
  )
color = alt.condition(selection, alt.Color('Origin:N', legend=None),
                      alt.value('lightgray'))

scatter = alt.Chart(cars).mark_point().encode(
    x='Horsepower:Q', y='Miles_per_Gallon:Q', color=color, tooltip='Name:N')

legend = alt.Chart(cars).mark_point().encode(
    y=alt.Y('Origin:N', axis=alt.Axis(orient='right')),
    color=color).add_selection(selection)

scatter | legend

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:8 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
mattijncommented, Nov 14, 2019

Your example is working for me:

import altair as alt
from vega_datasets import data

cars = data.cars.url

selection = alt.selection_multi(
    fields=["Origin"], init=[{"Origin": "Europe"}, {"Origin": "Japan"}]
)
color = alt.condition(
    selection, alt.Color("Origin:N", legend=None), alt.value("lightgray")
)

scatter = (
    alt.Chart(cars)
    .mark_point()
    .encode(x="Horsepower:Q", y="Miles_per_Gallon:Q", color=color, tooltip="Name:N")
)

legend = (
    alt.Chart(cars)
    .mark_point()
    .encode(y=alt.Y("Origin:N", axis=alt.Axis(orient="right")), color=color)
    .add_selection(selection)
)

scatter | legend
image
0reactions
jakevdpcommented, Nov 14, 2019

The jupyterlab vega extension uses an older version of vega/vega-lite, I belive. Colab is up-to-date as of the Altair 3.2 release. I checked with the vega-editor, and the chart does not work in Vega-Lite 4 either.

I suspect it was a regression in Vega-Lite somewhere along the way; may be worth opening an issue there.

Read more comments on GitHub >

github_iconTop Results From Across the Web

altair.selection_multi — Altair 4.2.0 documentation
"multi" – to select multiple discrete data value; the first value is ... Initialize the selection with a mapping between projected channels or...
Read more >
How to have init values for a selection interval in Altair when ...
I'm trying to have initial values for a selection_interval in a Altair plot, but I've been unable to figure it out. Here is...
Read more >
altair.MultiSelection — Altair 4.1.0 documentation
"multi" – to select multiple discrete data value; the first value is selected on click and additional values toggled on shift- click ....
Read more >
Grid multi-select, initialize with multiple selected - EJ 2 Forums
I'm trying to use the enableSimpleMultiRowSelection of the GridComponent, and want to select rows initially for my component when it renders.
Read more >
Multiple Unit Selection in Unity || RTS, Diablo, City Builder ...
In this video we'll build a simple unit selection system.It includes single selection, and multiple selections with shift click and also a ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found