Python 牵牛星可以';不要创建选择的组合
我想创建一个交互式“图例”,允许我选择不同列中的数据组合。 例如,如果我们有以下数据:Python 牵牛星可以';不要创建选择的组合,python,data-visualization,visualization,vega-lite,altair,Python,Data Visualization,Visualization,Vega Lite,Altair,我想创建一个交互式“图例”,允许我选择不同列中的数据组合。 例如,如果我们有以下数据: data = { 'type1': [1, 0, 0, 1, 0, 1], 'type2': [1, 1, 1, 1, 0, 0], 'type3': [1, 0, 0, 1, 1, 0], 'id': ['test1', 'test2', 'test3', 'test4', 'test5', 'test6'], 'fuel': [1, 10, 30, 50, 25,
data = {
'type1': [1, 0, 0, 1, 0, 1],
'type2': [1, 1, 1, 1, 0, 0],
'type3': [1, 0, 0, 1, 1, 0],
'id': ['test1', 'test2', 'test3', 'test4', 'test5', 'test6'],
'fuel': [1, 10, 30, 50, 25, 20]
}
df = pd.DataFrame(data, columns=['id', 'fuel', 'type1', 'type2', 'type3'])
结果是:我现在希望能够可视化不同“类型”的组合。
类似这样的内容:
因此,如果只切换type1,则只应显示test6。
如果切换所有类型,则只应显示test4和test1。
理想的解决方案是用一个图例来实现这一点,有可能实现吗?
也可以像我在上图中尝试的那样使用多个图表来实现这一点吗?
我似乎无法理解这一点。一种方法是为每列创建一个选择,并绑定到一个复选框。例如:
import altair as alt
import pandas as pd
data = {
'type1': [1, 0, 0, 1, 0, 1],
'type2': [1, 1, 1, 1, 0, 0],
'type3': [1, 0, 0, 1, 1, 0],
'id': ['test1', 'test2', 'test3', 'test4', 'test5', 'test6'],
'fuel': [1, 10, 30, 50, 25, 20]
}
sel = [
alt.selection_single(bind=alt.binding_checkbox(name=field), fields=[field], init={field: False})
for field in ['type3', 'type2', 'type1']
]
df = pd.DataFrame(data, columns=['id', 'fuel', 'type1', 'type2', 'type3'])
alt.Chart(df).transform_calculate(
type1='toBoolean(datum.type1)',
type2='toBoolean(datum.type2)',
type3='toBoolean(datum.type3)',
).mark_point().encode(
x='id',
y='fuel',
opacity=alt.condition(sel[0] & sel[1] & sel[2], alt.value(1), alt.value(0))
).add_selection(
*sel
)
非常感谢您!!这正是我需要的。