简体   繁体   中英

Remove legend for points in Altair

I'm following this example https://altair-viz.github.io/gallery/multiline_highlight.html , and added text points. My lines have both strokeDash and color.

import altair as alt
from vega_datasets import data

source = data.stocks()

highlight = alt.selection(type='single', on='mouseover',
                          fields=['symbol'], nearest=True)

base = alt.Chart(source).encode(
    x='date:T',
    y='price:Q',
    color='symbol:N',
    strokeDash='symbol:N'
)

points = base.mark_circle().encode(
    opacity=alt.value(0)
).add_selection(
    highlight
).properties(
    width=600
)

lines = base.mark_line().encode(
    size=alt.condition(~highlight, alt.value(1), alt.value(3))
)

points + lines

I'd like the legend to only show the dashed and colored lines, not the others (legend for text and points iiuc)

Is it possible to completely remove the extra legends from a chart?

It is enough to explicitly remove the legend on points and text by setting the color and strokeDash attributes

import altair as alt
from vega_datasets import data

source = data.stocks()

highlight = alt.selection(type='single', on='mouseover',
                          fields=['symbol'], nearest=True)

base = alt.Chart(source).encode(
    x='date:T',
    y='price:Q',
    color='symbol:N',
    strokeDash='symbol:N'
)

points = base.mark_point().encode(
    opacity=alt.value(0),
    color=alt.Color('symbol:N', legend=None),
    strokeDash=alt.StrokeDash('symbol:N', legend=None)
).add_selection(
    highlight
).properties(
    width=600
)

lines = base.mark_line().encode(
    size=alt.condition(~highlight, alt.value(1), alt.value(3))
)

text = lines.mark_text(
    align='left',
    baseline='middle',
    dx=7
).encode(
    text='symbol',
    color=alt.Color('symbol:N', legend=None),
    strokeDash=alt.StrokeDash('symbol:N', legend=None)
)

(lines + points + text).resolve_scale(color='independent', strokeDash='independent')

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