Python Altair rendering error - Javascript Error: too much recursion

Viewed 107

I am using Altair charts for Python with Jupyter Notebooks, but one of the (rendering?) components crashes if I increase the amount of data points:

import altair as alt
import datetime
import pandas as pd
import random

def add_minutes(tm, minutes_to_add):
    fulldate = datetime.datetime(100, 1, 1, tm.hour, tm.minute, tm.second)
    fulldate = fulldate + datetime.timedelta(minutes=minutes_to_add)
    return fulldate.time()

time_sort_order = []

render_hours = 13

def build_df():
    value_list = []
    start_time = datetime.time(21, 0, 0)
    for n in range(60*render_hours):
        time_to_fetch = add_minutes(start_time, 1*n)
        time_sort_order.append(str(time_to_fetch))
        value_list.append({
            "time": str(time_to_fetch),
            "value": random.randint(1, 5)
        })
    return pd.DataFrame(value_list)

df = build_df()

alt.Chart(df).mark_rect().encode(
    x=alt.X('time', sort=time_sort_order),
    color='value:Q'
)

If I set render_hours = 13: Everything works/renders normally

If I set render_hours = 14: I get a blank output

If I set render_hours = 24:

Javascript Error: too much recursion This usually means there's a typo in your chart specification. See the javascript console for the full traceback.

1 Answers

It looks like the renderer can't handle an explicit sort order with hundreds of entries; that is probably a bug in Vega, but it's also a bit of an anti-pattern.

Try doing something like this instead:

alt.Chart(df.reset_index()).mark_rect().encode(
    x=alt.X('time', sort=alt.SortField('index')),
    color='value:Q'
)
Related