使用 altair graph API 创建三个不同的图表,然后根据 altair 文档将它们合并。
(underlay+base+overlay).save("layeredChart.html")
生成一个名为 layeredChart.html 的 html 文件
打开 html 文件时出现错误:
JavaScript 错误:重复的信号名称:“selector002_tuple” 这通常意味着您的图表规范中有错字。有关完整的回溯,请参阅 javascript 控制台。
尽管在 jupyter notebook 上运行良好,但使用 altair 生成 html 文件时出错的原因可能是什么?
代码:
import altair as alt
#altair plot
alt.data_transformers.disable_max_rows()
#Selection tool
selection = alt.selection_single(fields = ['Country/Region'])
#Underlay
base = alt.Chart(de_long).mark_line(strokeWidth=4,opacity=0.7).encode(
x = alt.X('Day'),
y = alt.Y('De',scale=alt.Scale(type='log')),
color = alt.Color('Country/Region',legend=None)
).properties(
width=800,
height=650
).interactive()
print(alt.renderers.names())
#Chart
chart1 = base.encode(
color=alt.condition(selection,'Country/Region:N',alt.value('lightgray'))).add_selection(selection)
#Overlay
overlay = base.encode(
color = 'Country/Region',
opacity = alt.value(0.5),
tooltip = ['Country/Region:N','Name:N']
).transform_filter(selection)
finalChart = (base+chart1+overlay)
finalChart.save("final.html")