Altair 双下拉菜单

Altair double dropdown menu

我想创建一个带有两个下拉菜单的绘图。线条的不透明度应取决于 both 下拉菜单中的选择。

理想情况下,第二个下拉菜单(下面的 "dropdown_symbol")的可能内容应取决于第一个下拉菜单中的选择(当在第一个下拉菜单中选择 "category_2" 时,仅显示 "AMZM" 和 "IBM" 在第二个下拉列表中)。

不幸的是,不透明度既不能正常工作,也不能限制可能的内容。这是我的示例代码:

import altair as alt
from vega_datasets import data

source = data.stocks()
source.symbol.value_counts()


source["category"] = "category_1"
source.loc[source["symbol"].isin(["AMZN", "IBM"]), "category"] = "category_2"


dropdown_category = alt.binding_select(options=list(source["category"].unique()), name=" ")
dropdown_symbol = alt.binding_select(options=list(source["symbol"].unique()), name=" ")

selection_category = alt.selection_single(fields=["category"], bind=dropdown_category)
selection_symbol = alt.selection_single(fields=["symbol"], bind=dropdown_symbol)

chart = alt.Chart(source).mark_line().encode(
    x='date',
    y='price',
    color='symbol',
    opacity=alt.condition(
        selection_category & selection_symbol,
        alt.value(1),
        alt.value(0.1)
)).add_selection(selection_symbol, selection_category)

chart

I want to create a plot with two dropdown menus. The opacity of the lines should depend on the selection in both dropdown menus.

如果您希望不透明度取决于两个下拉菜单中的选择,您可以使用 alt.condition 语句并使用布尔运算符传递两个选择对象;例如:

opacity=alt.condition(
        selection1 & selection2,
        alt.value(1),
        alt.value(0.1))

仅当点在两个选择范围内时才会选择第一个值。或者,

opacity=alt.condition(
        selection1 | selection2,
        alt.value(1),
        alt.value(0.1))

如果该点在 至少一个 个选择范围内,将选择第一个值。

请注意,以这种方式使用多项选择时,空选择的行为存在一个已知错误;请参阅 https://github.com/altair-viz/altair/issues/1759 和其中的参考文献。

Ideally the possible content of the second dropdown menu ("dropdown_symbol" below) should depend on the selection in the first dropdown (when "category_2" is selected in the first dropdown, only show "AMZM" and "IBM" in the second dropdown).

在 Altair 中,下拉菜单的内容无法根据另一个下拉菜单中的选择动态更改。

要更改此功能,必须将功能添加到 Vega-Lite:您可以提交功能请求 here

我一直在努力做一些类似的工作,我想还有其他人和我一样,所以让我post在这里post解决你问题的第一部分。

跟进 jakevdp 的回答,其中提到了 Vega-lite 中的错误:描述了一种解决方法 here。但是,我找不到将其转换为正确的 Altair Python 代码的方法,因此我选择将条件编写为 Json/embedded 字典格式的 Vega 规范。这里的关键是包含 test 字段。

此外,您可能需要一种重置选择的方法。这可以通过在选项中包含 None 来完成,如 .

所述

下面的代码适用于 Altair 4.1.0。

import altair as alt
from vega_datasets import data

source = data.stocks()
source.symbol.value_counts()

source["category"] = "category_1"
source.loc[source["symbol"].isin(["AMZN", "IBM"]), "category"] = "category_2"

cat_values = list(source["category"].unique())
sym_values = list(source["symbol"].unique())

cat_options = [None] + cat_values
sym_options = [None] + sym_values

cat_labels = ["All"] + cat_values
sym_labels = ["All"] + sym_values

dropdown_category = alt.binding_select(options=cat_options, labels=cat_labels, name=" ")
dropdown_symbol = alt.binding_select(options=sym_options, labels=sym_labels, name=" ")

selection_category = alt.selection_single(fields=["category"], bind=dropdown_category, name="cat")
selection_symbol = alt.selection_single(fields=["symbol"], bind=dropdown_symbol, name="sym")

op_condition = {"condition":
                {"test":
                 {"and":
                  [{"selection": "cat"},
                   {"selection": "sym"}]},
                    "value": 1},
                "value": 0.1}

chart = alt.Chart(source).mark_line().encode(
    x='date',
    y='price',
    color='symbol',
    opacity=op_condition
).add_selection(selection_symbol, selection_category)

chart