Altair interactive line plot, make line pop and highlighted when clicking icon on the right

不羁岁月 提交于 2019-12-24 01:49:13

问题


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?

Attached is the code.

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

回答1:


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.

For example:

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


来源:https://stackoverflow.com/questions/55794391/altair-interactive-line-plot-make-line-pop-and-highlighted-when-clicking-icon-o

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!