Altair 交互式线图,单击右侧图标时使线弹出并突出显示

2024-01-09

我一直在尝试在 jupyter 实验室上使用 Altair 制作一些交互式绘图。

I had reached this stage where the results is below. enter image description here

正如您所看到的,当该行突出显示时,它不会弹出到前面。如何让它弹出到前面?

附件是代码。

import altair as alt
source = df
selection = alt.selection_multi(fields=['class'], on='click')    
color = alt.condition(selection,
                      alt.Color('class:O', legend=None,
                      scale=alt.Scale(scheme='category10')),
                      alt.value('lightgray'))

base = alt.Chart(source).mark_line(point=True, size=10).encode(
    x='x',
    y='y',
    color=color
).properties(
    width=800,
    height=900
).interactive()

legend = alt.Chart(source).mark_point(filled=True, size=200).encode(
    y=alt.Y('class:O'),
    color=color
).add_selection(
selection
)

base | legend

无法根据选择更改线的 z 顺序。但是,您可以使用一个技巧来创建类似的效果,即使用显示所有数据的静态背景以及根据选择进行过滤的前景。

例如:

background = alt.Chart(source).mark_line(point=True, size=10).encode(
    x='x',
    y='y',
    color=alt.value('lightgray')
).properties(
    width=800,
    height=900
)

foreground = background.encode(
    color=alt.Color('class:O', legend=None,
                    scale=alt.Scale(scheme='category10'))
).transform_filter(
    selection
)


legend = alt.Chart(source).mark_point(filled=True, size=200).encode(
    y=alt.Y('class:O'),
    color=color
).add_selection(
    selection
)

(background + foreground) | legend
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Altair 交互式线图,单击右侧图标时使线弹出并突出显示 的相关文章

随机推荐