1

使用 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")
4

1 回答 1

1

此错误通常意味着您add_selection()在多个图层上使用相同的选择进行调用,而 Vega-Lite 渲染器不支持。

这是此错误的最小可重现示例:

import altair as alt
import pandas as pd

df = pd.DataFrame({'x': range(10)})
selection = alt.selection_single()
base = alt.Chart(df).encode(x='x').add_selection(selection)
base.mark_line() + base.mark_point()
Javascript Error: Duplicate signal name: "selector001_tuple"
This usually means there's a typo in your chart specification. See the javascript console for the full traceback.

修复它的方法是仅将选择添加到其中一个图层;例如:

base = alt.Chart(df).encode(x='x')
base.mark_line() + base.mark_point().add_selection(selection)

在此处输入图像描述

于 2020-04-21T12:43:37.087 回答