【问题标题】:Remove legend for points in Altair删除 Altair 中点的图例
【发布时间】:2022-07-29 01:28:08
【问题描述】:

我正在关注这个示例https://altair-viz.github.io/gallery/multiline_highlight.html,并添加了文本点。我的线条既有 strokeDash 又有颜色。

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

我希望图例只显示虚线和彩色线,而不是其他(文本和点 iiuc 的图例)

是否可以从图表中完全删除多余的图例?

【问题讨论】:

    标签: python altair


    【解决方案1】:

    通过设置colorstrokeDash 属性显式删除pointstext 上的图例就足够了

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

    【讨论】:

      猜你喜欢
      • 2019-04-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-15
      • 2020-07-03
      • 1970-01-01
      • 2020-09-07
      • 2019-04-25
      相关资源
      最近更新 更多