I'd like to be able to toggle the display of the points on and off in the below chart. The 2 lines are the means of the points in groups 1 and 2. I thought there would be a way to do this using interactive
but cannot find any examples. Any help is much appreciated.
import math
import numpy as np
import pandas as pd
import altair as alt
x = np.arange(0,math.pi,0.1);
y = np.sin(x);
a, b = -0.2, 0.2
summary_df=[]
for i in range(0,2):
for j in range(0,5):
rand_y = (b - a)*np.random.rand(len(y)) + a
df = pd.DataFrame({
'group': i,
'batch': j,
'x': x,
'y': y+rand_y
})
summary_df.append(df)
summary_df = pd.concat(summary_df)
base = alt.Chart(
summary_df
).properties(
width=200,
height=400
)
mean_selection = alt.selection_multi(fields=['group'], bind='legend')
mean_line = base.mark_line(size=2).encode(
x=alt.X('x:Q'),
y=alt.Y('y:Q', aggregate='mean', axis=alt.Axis(title='y')),
color='group:N',
opacity=alt.condition(mean_selection, alt.value(1), alt.value(0.2))
).add_selection(
mean_selection
).interactive()
all_selection = alt.selection_multi(fields=['group'], bind='legend')
all_points = base.mark_square(size=10).encode(
y=alt.Y('y:Q', axis=alt.Axis(title='y')),
x=alt.X('x:Q',),
color='group:N',
tooltip='batch:N',
opacity=alt.condition(all_selection, alt.value(1), alt.value(0.2))
).add_selection(
all_selection
).interactive()
(mean_line+all_points)