简体   繁体   中英

Size legend disappears when adding selection in scatterplot

This is how the legend must appear:

在此处输入图片说明

But I am getting this instead:

在此处输入图片说明

This is my code. When I don't add the selection, the legend appears normal. Am I forgetting to do any binding?

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

This looks like it may be a bug in Vega-Lite. To get around it, you could instead condition on an opacity encoding and keep the color unchanged:

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

Another option would be to add a stroke property to the mark, which will be reflected in the legend.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM