问题
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