问题
When I plot a layered chart consisting of two groups of lines, the tooltips in one layer do not show up. This also occurs in the VL editor. Any insight to why this is happening would be much appreciated.
Here is a reproducible example to demonstrate the issue (in reality I have more lines in the first layer):
Altair version 4.0.0
df=pd.DataFrame({'school_code': ['AQUI', 'Board'] * 5, 'y4_rate': [.1, .2, .3, .4, .5, .1, .2, .3, .4, .5],
'cohort_year': ['1', '1', '2', '2','3', '3', '4', '4', '5', '5']})
sch=alt.Chart(df).mark_line(point=True).encode(
x=alt.X('cohort_year', axis=alt.Axis(labels=False)),
y=alt.Y('y4_rate', axis=alt.Axis(format='.0%'), title='Percentage of Students'),
color=alt.Color('school_code', title=None, legend=alt.Legend(labelFontSize=15, titleFontSize=20)),
tooltip=[alt.Tooltip('y4_rate', title='percentage of students', format='.0%')]
).transform_filter(alt.datum.school_code != 'Board')
brd=alt.Chart(df).mark_line(point=True).encode(
x=alt.X('cohort_year', axis=alt.Axis(labels=False)),
y=alt.Y('y4_rate', axis=alt.Axis(format='.0%'), title='Percentage of Students'),
color=alt.Color('school_code', title=None, legend=alt.Legend(labelFontSize=15, titleFontSize=20),scale=alt.Scale(range=['black'])),
tooltip=[alt.Tooltip('y4_rate', title='percentage of students', format='.0%')]
).transform_filter(alt.datum.school_code == 'Board')
alt.layer(sch, brd).resolve_scale(color='independent').properties(width=700, height=400).interactive()
回答1:
as mentioned by @campo in comments you must use interactive separately for each chart.
Now both tooltips will show up.
df=pd.DataFrame({'school_code': ['AQUI', 'Board'] * 5, 'y4_rate': [.1, .2, .3, .4, .5, .1, .2, .3, .4, .5],
'cohort_year': ['1', '1', '2', '2','3', '3', '4', '4', '5', '5']})
sch=alt.Chart(df).mark_line(point=True).encode(
x=alt.X('cohort_year', axis=alt.Axis(labels=False)),
y=alt.Y('y4_rate', axis=alt.Axis(format='.0%'), title='Percentage of Students'),
color=alt.Color('school_code', title=None, legend=alt.Legend(labelFontSize=15, titleFontSize=20)),
tooltip=[alt.Tooltip('y4_rate', title='percentage of students', format='.0%')]
).transform_filter(alt.datum.school_code != 'Board').interactive() #<<< Here
brd=alt.Chart(df).mark_line(point=True).encode(
x=alt.X('cohort_year', axis=alt.Axis(labels=False)),
y=alt.Y('y4_rate', axis=alt.Axis(format='.0%'), title='Percentage of Students'),
color=alt.Color('school_code', title=None, legend=alt.Legend(labelFontSize=15, titleFontSize=20),scale=alt.Scale(range=['black'])),
tooltip=[alt.Tooltip('y4_rate', title='percentage of students', format='.0%')]
).transform_filter(alt.datum.school_code == 'Board').interactive() #<<< Here
alt.layer(sch, brd).resolve_scale(color='independent').properties(
width=700, height=400
) #.interactive() # commented out this.
来源:https://stackoverflow.com/questions/59671294/altair-unable-to-get-tooltips-for-one-layer-in-a-line-chart