How to display/save a Layered graph in altair by running code from Python console

645 Views Asked by At

After creating three different charts with altair graph API and then merging them as per altair documentation.

(underlay+base+overlay).save("layeredChart.html")

An html file is generated with name layeredChart.html

On opening the html file error comes:

JavaScript Error: Duplicate signal name: "selector002_tuple" This usually means there's a typo in your chart specification. See the javascript console for the full traceback.

What can be the reason for error in html file generation with altair though works fine with jupyter notebook??

Code:

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")
1

There are 1 best solutions below

0
On

This error generally means that you've called add_selection() with the same selection on multiple layers, which is not supported by the Vega-Lite renderer.

Here is a Minimal Reproducible Example of this error:

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.

The way to fix it is to add the selection to only one of the layers; for example:

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

enter image description here