在散点图中添加选择时,尺寸图例消失

时间:2019-03-12 16:11:58

标签: python plot selection legend altair

这是图例必须出现的方式:

enter image description here

但是我得到的却是这个:

enter image description here

这是我的代码。当我不添加选择时,图例显示为正常。我忘了做绑定吗?

input_dropdown = alt.binding_select(options=['LIIDC', 'LIGIC', 'LIADC', 'LMECC', 'LIMCC', 'LTISC'])
selection = alt.selection_single(fields=['Carrera'], bind=input_dropdown, name="Filtrar")
color = alt.condition(selection,
                    alt.Color('Carrera:N', scale=alt.Scale(scheme='dark2')),
                    alt.value('#00000000'))

alt.Chart(df).mark_point(filled=True, size=100).encode(
    x= alt.X('Promedio de Preparatoria', scale = alt.Scale(domain=[6, 10.5])),
    y= alt.Y('Resultado Ceneval', scale = alt.Scale(domain=[900, 1300])),
    color=color,
    size=alt.Size('Beca Promedio', scale=alt.Scale(range=[30, 200]))
).add_selection(
    selection
).properties(
    width = 900, 
    height = 600,
    title = 'Promedio de Preparatoria contra Resultado Ceneval por Carrera'
).interactive()

1 个答案:

答案 0 :(得分:1)

这似乎是Vega-Lite中的错误。要解决此问题,您可以改用不透明度编码并保持颜色不变:

input_dropdown = alt.binding_select(options=['LIIDC', 'LIGIC', 'LIADC', 'LMECC', 'LIMCC', 'LTISC'])
selection = alt.selection_single(fields=['Carrera'], bind=input_dropdown, name="Filtrar")

alt.Chart(df).mark_point(filled=True, size=100).encode(
    x= alt.X('Promedio de Preparatoria', scale = alt.Scale(domain=[6, 10.5])),
    y= alt.Y('Resultado Ceneval', scale = alt.Scale(domain=[900, 1300])),
    color=alt.Color('Carrera:N', scale=alt.Scale(scheme='dark2')),
    opacity=alt.condition(selection, alt.value(0.8), alt.value(0)),
    size=alt.Size('Beca Promedio', scale=alt.Scale(range=[30, 200]))
).add_selection(
    selection
).properties(
    width = 900, 
    height = 600,
    title = 'Promedio de Preparatoria contra Resultado Ceneval por Carrera'
).interactive()

另一种选择是向标记添加笔触属性,该属性将反映在图例中。