Altair交互式线条图,单击右侧的图标时,线条会弹出并突出显示 [英] Altair interactive line plot, make line pop and highlighted when clicking icon on the right

查看:206
本文介绍了Altair交互式线条图,单击右侧的图标时,线条会弹出并突出显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直试图在jupyter实验室中使用Altair制作一些互动情节.

I had been trying to make with some interactive plot using Altair on jupyter lab.

我到了这个阶段,结果低于下面的水平.

I had reached this stage where the results is below.

如您所见,该行突出显示时不会弹出到最前面.如何使其弹出到前面?

As you can see, the line doesnt pop to the front when its highlighted. How do I make it pop to the front?

附带的代码.

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顺序.但是,创建类似效果的一种技巧是使用显示所有数据的静态背景以及在选择内容上过滤的前景.

There is no way to change the z-order of a line based on a selection. But one trick you can play to create a similar effect is to use a static background showing all the data, along with a foreground filtered on the selection.

例如:

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

这篇关于Altair交互式线条图,单击右侧的图标时,线条会弹出并突出显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆