将动态查询应用于祭坛条形图

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'
)

我正在尝试将动态查询功能与 Altair Gallery 中的示例之一相结合。 然而,我 运行 遇到了这个错误,它不是很具有描述性(在 Javascript 控制台中也找不到任何东西吗?)

感谢任何帮助

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.

您忘记将选择添加到图表中:

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
)