Altair layered charts: How to sum over charts?

Viewed 204

I have multiple charts that are in a dictionary index by a parameter and I would like to plot them together in a layered chart by summing over the index. Here is an example of what I want to do and what I have tried:

chart = dict()
lst = [2,4,6,8]
chart[lst[0]] + chart[lst[1]] + chart[lst[2]] + chart[lst[3]]

The above code works but I would like to have this automated because my list changes all the time and I don't want to be typing that sum manually every time. I have tried this

sum(chart[i] for i in lst)

but then I get an error saying that list index out of range. I guess the python sum is not made for this kind of things but I don't know which other function is appropriate.

1 Answers

The chart1 + chart2 operator is a shorthand for alt.layer(chart1, chart2). If you want to layer multiple charts, you can use alt.layer directly:

alt.layer(*(chart[i] for i in lst))
Related