2

The following code block comes entirely from Altair documentation, it's one of the example graphs.

import altair as alt
from vega_datasets import data

source = data.population.url

select_year = alt.selection_point(
    name="Year",
    fields=["year"],
    bind=alt.binding_range(min=1900, max=2000, step=10, name="Year"),
    value={"year": 2000},
)

alt.Chart(source).mark_bar().encode(
    x=alt.X("sex:N", axis=alt.Axis(labels=False, title=None, ticks=False)),
    y=alt.Y("people:Q", scale=alt.Scale(domain=(0, 12000000)), title="Population"),
    color=alt.Color(
        "sex:N",
        scale=alt.Scale(domain=("Male", "Female"), range=["steelblue", "salmon"]),
        title="Sex",
    ),
    column=alt.Column("age:O", title="Age"),
).properties(
    width=20,
    title="U.S. Population by Age and Sex"
).add_params(
    select_year
).transform_calculate(
    "sex", alt.expr.if_(alt.datum.sex == 1, "Male", "Female")
).transform_filter(
    select_year
).configure_facet(
    spacing=8
)

Even though it's an official example, on my machine it results in an error:

Javascript Error: Cannot read properties of undefined (reading 'length')
This usually means there's a typo in your chart specification. See the javascript console for the full traceback.

I'm running this in JupyterLab v3.4.4 in Chrome, macOS 12.6, conda with altair v5.0.1. This graph uses alt.selection_point which was only added in altair v5. Other graphs are displaying just fine.

I originally ran this as part of a different notebook, but later created a new notebook on a same kernel and pasted ONLY this example there, no other code.

Why could this be happening?

  • No reference, but it displayed correctly in my environment. Windows11,jupyterLab:4.0.2,altair:5.01 – r-beginners Jun 09 '23 at 12:01
  • @r-beginners you won't believe it - updating to jupyterlab 4.0.2 worked! thank you very much, for some reason the jupyter version completely slipped my mind. It's interesting how you forget such basic things. – RavenbornJB Jun 09 '23 at 13:42
  • This worked for me as well. Went from jupyterlab >3.6.x to 4.0.x and the issue was resolved (in VS code + notebooks on Mac with altair 5.0.1) – Thomas Jun 27 '23 at 07:27

1 Answers1

1

As it turns out, it was the JupyterLab version. Updating to version 4.0.2 resolved the issue. I'm an idiot :D

Edit: Actually, the issue still occurs sometimes. I have no clue why, don't see any correlation with my actions.