[Python] How to resize/fit Altair chart in Quarto dashboard container?

Discussão em 'Python' iniciado por Stack, Julho 9, 2025.

  1. Stack

    Stack Membro Participativo

    The Quarto docs state that Altair charts resize themselves to fit their container within dashboards.

    My experience is that this is true for alt.Chart(), but not if I concatenate multiple charts via alt.hconcat().

    ---
    title: "Test"
    format: dashboard
    ---

    ```{python}
    #| title: alt.chart()

    import altair as alt
    from vega_datasets import data
    source = data.iowa_electricity()
    alt.Chart(source).mark_area(opacity=0.3).encode(
    x="year:T",
    y=alt.Y("net_generation:Q").stack(None),
    color="source:N"
    )
    ```

    ```{python}
    #| title: Chart concatenation

    import altair as alt
    from vega_datasets import data
    source = data.iowa_electricity()
    chart1 = alt.Chart(source).mark_area(opacity=0.3).encode(
    x="year:T",
    y=alt.Y("net_generation:Q").stack(None),
    color="source:N"
    )

    chart2 = alt.Chart(source).mark_area(opacity=0.3).encode(
    y="year:T",
    x=alt.Y("net_generation:Q").stack(None),
    color="source:N"
    )

    alt.hconcat(chart1, chart2)
    ```


    [​IMG]

    Is there a way to fit a concatenated chart to its container?

    Continue reading...

Compartilhe esta Página