如何使用鼠标悬停使其他行变灰?
how to grey out other lines using Mouse hover?
当我将鼠标悬停在其中一条线上时,我试图使其他线变灰,到目前为止我有这个:
from sklearn import datasets
data_wine = datasets.load_wine (as_frame = True).frame
new_data = data_wine.drop (['proline', 'magnesium'], axis = 1)
new_data = new_data.reset_index().melt(id_vars = ['index', 'target'])
hover = alt.selection(
type="single", on="mouseover", fields=["variable"], nearest=True
)
lineplot = alt.Chart(new_data).mark_line().encode(
alt.X("variable:N"),
alt.Y("value:Q"),
alt.Color ('target:N'),
alt.Detail ('index:N'),
).properties(width = 1000)
# nearest point
point = lineplot.mark_circle().encode(
opacity=alt.value(0)
).add_selection(hover)
# highlight
singleline = lineplot.mark_line().encode(
size=alt.condition(~hover, alt.value(0.5), alt.value(3))
)
point+singleline
它看起来像这样,当鼠标悬停时尺寸发生变化,我无法用颜色替换尺寸:
我怎样才能做到这一点?
new_data = data_wine.drop (['proline', 'magnesium'], axis = 1)
new_data = new_data.reset_index().melt(id_vars = ['index', 'target'])
highlight = alt.selection(type='single', on='mouseover', fields=['target'], nearest=False, bind='legend')
selection = alt.selection_multi(fields=['target'], bind='legend', on='mouseover')
lineplot=alt.Chart(new_data).mark_line().encode(
alt.X("variable:N"),
alt.Y("value:Q"),
alt.Color ('target:N'),
alt.Detail ('index:N'),
).properties(width = 1000)
# nearest point
point = lineplot.mark_circle().encode(
opacity=alt.value(0)
).add_selection(highlight)
#highlight
singleline = lineplot.mark_line().encode(
opacity=alt.condition(selection, alt.value(0.7), alt.value(0.03))
#size=alt.condition(~highlight, alt.value(1), alt.value(3))
).add_selection(selection)
point + singleline
当我将鼠标悬停在其中一条线上时,我试图使其他线变灰,到目前为止我有这个:
from sklearn import datasets
data_wine = datasets.load_wine (as_frame = True).frame
new_data = data_wine.drop (['proline', 'magnesium'], axis = 1)
new_data = new_data.reset_index().melt(id_vars = ['index', 'target'])
hover = alt.selection(
type="single", on="mouseover", fields=["variable"], nearest=True
)
lineplot = alt.Chart(new_data).mark_line().encode(
alt.X("variable:N"),
alt.Y("value:Q"),
alt.Color ('target:N'),
alt.Detail ('index:N'),
).properties(width = 1000)
# nearest point
point = lineplot.mark_circle().encode(
opacity=alt.value(0)
).add_selection(hover)
# highlight
singleline = lineplot.mark_line().encode(
size=alt.condition(~hover, alt.value(0.5), alt.value(3))
)
point+singleline
它看起来像这样,当鼠标悬停时尺寸发生变化,我无法用颜色替换尺寸:
我怎样才能做到这一点?
new_data = data_wine.drop (['proline', 'magnesium'], axis = 1)
new_data = new_data.reset_index().melt(id_vars = ['index', 'target'])
highlight = alt.selection(type='single', on='mouseover', fields=['target'], nearest=False, bind='legend')
selection = alt.selection_multi(fields=['target'], bind='legend', on='mouseover')
lineplot=alt.Chart(new_data).mark_line().encode(
alt.X("variable:N"),
alt.Y("value:Q"),
alt.Color ('target:N'),
alt.Detail ('index:N'),
).properties(width = 1000)
# nearest point
point = lineplot.mark_circle().encode(
opacity=alt.value(0)
).add_selection(highlight)
#highlight
singleline = lineplot.mark_line().encode(
opacity=alt.condition(selection, alt.value(0.7), alt.value(0.03))
#size=alt.condition(~highlight, alt.value(1), alt.value(3))
).add_selection(selection)
point + singleline