Juan
Juan

Reputation: 103

Apply Dynamic Query to Altar Bar Chart

import pandas as pd
import altair as alt
from vega_datasets import data

source = data.barley()
unique_sites = source.site.unique().tolist()

selectSite = alt.selection_single(
    name='Select', # name the selection 'Select'
    fields=['site'], # limit selection to the site column
    init={'site': source.site[0]}, # use first entry as initial value
    bind=alt.binding_select(options=unique_sites) # bind to a menu of unique genre values
)

alt.Chart(source).mark_bar().encode(
    x='variety',
    y='sum(yield)',
    opacity=alt.condition(selectSite, alt.value(0.75), alt.value(0.05)),
    color='site'
)

I'm trying to combine the Dynamic Query feature with one of the examples from the Altair Gallery. I however run into this error, which is not very descriptive (could not find anything in the Javascript console as well?)

Any help is appreciated

Javascript Error: Cannot find a selection named "Select". This usually means there's a typo in your chart specification. See the javascript console for the full traceback.

Upvotes: 1

Views: 171

Answers (1)

jakevdp
jakevdp

Reputation: 86433

You forgot to add the selection to the chart:

alt.Chart(source).mark_bar().encode(
    x='variety',
    y='sum(yield)',
    opacity=alt.condition(selectSite, alt.value(0.75), alt.value(0.05)),
    color='site'
).add_selection(
    selectSite
)

enter image description here

Upvotes: 1

Related Questions