[Python] Altair fails when making faceted histogram with large dataset

Discussão em 'Python' iniciado por Stack, Abril 24, 2025.

  1. Stack

    Stack Membro Participativo

    When I try to make histogram with Altair with the code (n=1million) below,

    import polars as pl
    import numpy as np
    import altair as alt

    n = 1000000
    s = np.random.normal(0, 1, n)
    h = np.random.normal(0, 1, n)
    df = pl.DataFrame({"value": s, "h": h}).with_columns(
    pl.when(pl.col("h") > 0)
    .then(pl.lit("high"))
    .otherwise(pl.lit("low"))
    .alias("high_low"),
    pl.lit(0.5).alias("a"),
    )

    chart1 = (
    alt.Chart().mark_bar().encode(alt.X("value").bin(), y=alt.Y("count()", title="Pcs"))
    )

    chart2 = alt.Chart().mark_rule().encode(x="a")

    chart = (chart1 + chart2).facet(row="high_low", data=df)

    chart.save("./Python/trial/altair_histogram_trial16.png")


    Error below comes out and chart is not saved.

    <--- Last few GCs --->
    28[30152:000001797F667000] 14256 ms: Mark-Compact (reduce) 1399.8 (1400.9) -> 1399.0 (1401.1) MB, pooled: 0 MB, 546.81 / 0.00 ms (+ 0.4 ms in 0 steps since start of marking, biggest step 0.0 ms, walltime since start of marking 550 ms) (average mu = 0.23[30152:000001797F667000] 15447 ms: Mark-Compact (reduce) 1400.0 (1401.1) -> 1399.2 (1401.4) MB, pooled: 0 MB, 1067.80 / 0.00 ms (+ 0.1 ms in 0 steps since start of marking, biggest step 0.0 ms, walltime since start of marking 1071 ms) (average mu = 0.

    <--- JS stacktrace --->



    #
    # Fatal JavaScript out of memory: Reached heap limit
    #
    ==== C stack trace ===============================

    CrashForExceptionInNonABICompliantCodeRange [0x00007FF87E1E184B+1316923]
    wgpu_render_bundle_draw_indexed_indirect [0x00007FF87DECE157+1048900]
    wgpu_render_bundle_draw_indexed_indirect [0x00007FF87DF29D7A+1424743]
    wgpu_render_bundle_draw_indexed_indirect [0x00007FF87DEAB84C+907321]
    wgpu_render_bundle_draw_indexed_indirect [0x00007FF87DFB5EE7+1998548]
    wgpu_render_bundle_draw_indexed_indirect [0x00007FF87DFB3D30+1989917]
    CrashForExceptionInNonABICompliantCodeRange [0x00007FF87E3E68D7+3434695]
    CrashForExceptionInNonABICompliantCodeRange [0x00007FF87E3E7756+3438406]
    wgpu_render_bundle_draw_indexed_indirect [0x00007FF87DFDAC6D+2149466]
    CrashForExceptionInNonABICompliantCodeRange [0x00007FF87E93C6DD+9029325]
    CrashForExceptionInNonABICompliantCodeRange [0x00007FF87F2FC13A+19251498]
    (No symbol) [0x00007FF7FF5CB179]



    On the other hand, with smaller dataset (n=1K) below, I can make histogram. How can I make it with large dataset(1million)?

    OS: Windows11 Python: version 3.13.1 RAM: 64GB Altair: version 1.20.0

    import polars as pl
    import numpy as np
    import altair as alt

    n = 1000
    s = np.random.normal(0, 1, n)
    h = np.random.normal(0, 1, n)
    df = pl.DataFrame({"value": s, "h": h}).with_columns(
    pl.when(pl.col("h") > 0)
    .then(pl.lit("high"))
    .otherwise(pl.lit("low"))
    .alias("high_low"),
    pl.lit(0.5).alias("a"),
    )

    chart1 = (
    alt.Chart().mark_bar().encode(alt.X("value").bin(), y=alt.Y("count()", title="Pcs"))
    )

    chart2 = alt.Chart().mark_rule().encode(x="a")

    chart = (chart1 + chart2).facet(row="high_low", data=df)

    chart.save("./Python/trial/altair_histogram_trial17.png")



    [​IMG]

    Continue reading...

Compartilhe esta Página